| 1 | from typing import Iterator |
| 2 | from graphrag_sdk.document import Document |
| 3 | |
| 4 | class PDFLoader(): |
| 5 | """ |
| 6 | Load PDF |
| 7 | """ |
| 8 | |
| 9 | def __init__(self, path: str) -> None: |
| 10 | """ |
| 11 | Initialize loader |
| 12 | |
| 13 | Parameters: |
| 14 | path (str): path to PDF. |
| 15 | """ |
| 16 | |
| 17 | try: |
| 18 | import pypdf |
| 19 | except ImportError: |
| 20 | raise ImportError( |
| 21 | "pypdf package not found, please install it with " "`pip install pypdf`" |
| 22 | ) |
| 23 | |
| 24 | self.path = path |
| 25 | |
| 26 | def load(self) -> Iterator[Document]: |
| 27 | """ |
| 28 | Load PDF |
| 29 | |
| 30 | Returns: |
| 31 | Iterator[Document]: document iterator |
| 32 | """ |
| 33 | |
| 34 | from pypdf import PdfReader # pylint: disable=import-outside-toplevel |
| 35 | |
| 36 | reader = PdfReader(self.path) |
| 37 | yield from [ |
| 38 | Document(page.extract_text()) |
| 39 | for page in reader.pages |
| 40 | ] |