File size: 2,099 Bytes
ceeca9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be7b677
ceeca9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
from pdfdeal import Doc2X
from pdfdeal.file_tools import unzips

class PDFProcessor:
    def __init__(self, apikey):
        """
        Initialize PDFProcessor with API key
        """
        self.client = self._init_ocr(apikey)

    def _init_ocr(self, apikey):
        """
        Initialize Doc2X client
        """
        return Doc2X(apikey=apikey, debug=True, full_speed=True)

    def pdf2md(self, pdf_file, output_path):
        """
        Convert PDF to Markdown using Doc2X API.
        # """
        # if isinstance(pdf_file, list):
        #     output_names = [f"{ts}_{i}" for i in range(len(pdf_file))]
        # else:
        #     output_names = [ts]

        output_names = []
        for file in pdf_file:
            filename, file_ext = os.path.splitext(os.path.basename(file))
            output_names.append(filename)


        success, failed, flag = self.client.pdf2file(
            pdf_file=pdf_file,
            output_path=output_path,
            output_format="md",
            output_names=output_names
        )

        print(success)
        print(failed)
        print(flag)

        return success, failed, flag

    def unzip_markdown_files(self, success):
        """
        Unzip Markdown files generated by Doc2X API.
        """
        zips = [file for file in success if file.endswith(".zip")]
        success, failed, flag = unzips(zip_paths=zips)

        print(success)
        print(failed)
        print(flag)
        return success, failed, flag

    def process_pdf_files(self, pdf_files, output_path):
        """
        Process PDF files and unzip resulting markdown files
        """
        success, failed, flag = self.pdf2md(pdf_files, output_path)
        if success:
            return self.unzip_markdown_files(success)
        return None, failed, flag


if __name__ == "__main__":
    apikey = "sk-q183d0h5lbz38k3ex46m6pfxbao9p3h1"
    processor = PDFProcessor(apikey)
    
    # Example usage
    processor.process_pdf_files(
        pdf_files=["samples/111.pdf", "samples/222.pdf"],
        output_path="./output"
    )