File size: 800 Bytes
4c793dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path
import fitz


class DocumentProcessor:

    def process(self, file_path):

        path = Path(file_path)

        if path.suffix.lower() != ".pdf":
            raise ValueError(
                f"Unsupported file type: {path.suffix}"
            )

        return self.process_pdf(path)

    def process_pdf(self, path):

        document = fitz.open(path)

        pages = []

        for page_number, page in enumerate(document):

            pages.append({
                "page_number": page_number + 1,
                "text": page.get_text()
            })

        return {
            "document": {
                "filename": path.name,
                "format": "pdf",
                "page_count": len(document)
            },
            "pages": pages
        }