Parse a PDF file and generate various outputs including layout, markdown, content_list, etc. Args: f_path (str): The path to the PDF file to be parsed. reuse (bool): If True, skips processing if the target files already exist. Returns: s
(self, f_path: str, reuse: bool = True)
| 25 | self.data_reader = FileBasedDataReader('') |
| 26 | |
| 27 | def parse(self, f_path: str, reuse: bool = True) -> str: |
| 28 | """ |
| 29 | Parse a PDF file and generate various outputs including layout, markdown, content_list, etc. |
| 30 | |
| 31 | Args: |
| 32 | f_path (str): The path to the PDF file to be parsed. |
| 33 | reuse (bool): If True, skips processing if the target files already exist. |
| 34 | |
| 35 | Returns: |
| 36 | str: The path to the generated markdown file. |
| 37 | """ |
| 38 | |
| 39 | # TODO: |
| 40 | # 1. support file_list is a list of pdf urls |
| 41 | # 2. parallel parsing |
| 42 | |
| 43 | print(f'Processing file: {f_path}') |
| 44 | |
| 45 | file_name_no_suffix = os.path.splitext(os.path.basename(f_path))[0] |
| 46 | entry_md_file = os.path.join(self.markdown_dir, |
| 47 | f'{file_name_no_suffix}.md') |
| 48 | |
| 49 | if reuse and os.path.exists(entry_md_file): |
| 50 | print(f'File {entry_md_file} already exists. Skipping processing.') |
| 51 | return entry_md_file |
| 52 | |
| 53 | pdf_bytes = self.data_reader.read(f_path) |
| 54 | |
| 55 | ds = PymuDocDataset(pdf_bytes) |
| 56 | |
| 57 | # inference |
| 58 | if ds.classify() == SupportedPdfParseMethod.OCR: |
| 59 | infer_result = ds.apply(doc_analyze, ocr=True) |
| 60 | |
| 61 | # pipeline |
| 62 | pipe_result = infer_result.pipe_ocr_mode(self.img_writer) |
| 63 | |
| 64 | else: |
| 65 | infer_result = ds.apply(doc_analyze, ocr=False) |
| 66 | |
| 67 | # pipeline |
| 68 | pipe_result = infer_result.pipe_txt_mode(self.img_writer) |
| 69 | |
| 70 | # draw model result on each page |
| 71 | infer_result.draw_model( |
| 72 | os.path.join(self.markdown_dir, |
| 73 | f'{file_name_no_suffix}_model.pdf')) |
| 74 | |
| 75 | # draw layout result on each page |
| 76 | pipe_result.draw_layout( |
| 77 | os.path.join(self.markdown_dir, |
| 78 | f'{file_name_no_suffix}_layout.pdf')) |
| 79 | |
| 80 | # draw spans result on each page |
| 81 | pipe_result.draw_span( |
| 82 | os.path.join(self.markdown_dir, |
| 83 | f'{file_name_no_suffix}_spans.pdf')) |
| 84 |
no outgoing calls
no test coverage detected