Parse a PDF file and extract text and images. Args: pdf_path (str): The path to the PDF file. output_path (str): The directory to save the extracted content. model_lst (list): A list of models for processing the PDF. Returns: str: The full text extracte
(
pdf_path: str,
output_path: str = None,
model_lst: list = None,
save_file: bool = True,
)
| 95 | |
| 96 | |
| 97 | def parse_pdf( |
| 98 | pdf_path: str, |
| 99 | output_path: str = None, |
| 100 | model_lst: list = None, |
| 101 | save_file: bool = True, |
| 102 | ) -> str: |
| 103 | """ |
| 104 | Parse a PDF file and extract text and images. |
| 105 | |
| 106 | Args: |
| 107 | pdf_path (str): The path to the PDF file. |
| 108 | output_path (str): The directory to save the extracted content. |
| 109 | model_lst (list): A list of models for processing the PDF. |
| 110 | |
| 111 | Returns: |
| 112 | str: The full text extracted from the PDF. |
| 113 | """ |
| 114 | if save_file: |
| 115 | os.makedirs(output_path, exist_ok=True) |
| 116 | config_parser = ConfigParser( |
| 117 | { |
| 118 | "output_format": "markdown", |
| 119 | } |
| 120 | ) |
| 121 | converter = PdfConverter( |
| 122 | config=config_parser.generate_config_dict(), |
| 123 | artifact_dict=model_lst, |
| 124 | processor_list=config_parser.get_processors(), |
| 125 | renderer=config_parser.get_renderer(), |
| 126 | ) |
| 127 | rendered = converter(pdf_path) |
| 128 | full_text, _, images = text_from_rendered(rendered) |
| 129 | if save_file: |
| 130 | with open(pjoin(output_path, "source.md"), "w+", encoding="utf-8") as f: |
| 131 | f.write(full_text) |
| 132 | for filename, image in images.items(): |
| 133 | image_filepath = os.path.join(output_path, filename) |
| 134 | image.save(image_filepath, "JPEG") |
| 135 | with open(pjoin(output_path, "meta.json"), "w+") as f: |
| 136 | f.write(json.dumps(rendered.metadata, indent=4)) |
| 137 | |
| 138 | if not save_file: |
| 139 | return full_text, rendered |
| 140 | return full_text |
| 141 | |
| 142 | |
| 143 | def get_text_embedding( |
no test coverage detected