Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
refactor: add type hints to all backend/office/ functions
jinzhenj4116125e15c7
diff --git a/mineru/backend/office/docx_analyze.py b/mineru/backend/office/docx_analyze.pyindex a37e7394..31f6e3e0 100644--- a/mineru/backend/office/docx_analyze.py+++ b/mineru/backend/office/docx_analyze.py@@ -3,15 +3,12 @@ import timefrom io import BytesIOfrom loguru import logger-from mineru.backend.office.model_output_to_middle_json import result_to_middle_json-from mineru.model.docx.main import convert_binary+from ...model.docx.main import convert_binary+from .model_output_to_middle_json import result_to_middle_json-def office_docx_analyze(- file_bytes,- image_writer=None-):+def office_docx_analyze(file_bytes: bytes, image_writer: Any = None) -> dict:infer_start = time.time()file_stream = BytesIO(file_bytes)@@ -28,37 +25,32 @@ def office_docx_analyze(return middle_json, results-if __name__ == '__main__':++if __name__ == "__main__":# Resolve a default docx file relative to this script so the example# works no matter what the current working directory is when the# module is executed. Allow the user to override the path via a# command-line argument for even greater flexibility.- from pathlib import Pathimport argparse+ from pathlib import Pathscript_root = Path(__file__).resolve().parent.parent.parent.parentdefault_docx = script_root / "demo" / "office_docs" / "docx_01.docx"- parser = argparse.ArgumentParser(- description="Quick demo runner for office_docx_analyze"- )+ parser = argparse.ArgumentParser(description="Quick demo runner for office_docx_analyze")parser.add_argument("docx",nargs="?",default=str(default_docx),- help="path to docx file (defaults to demo/office_docs/docx_01.docx relative to project root)"- )- parser.add_argument(- "--output-images",- help="directory to write image outputs",- default="./output_images"+ help="path to docx file (defaults to demo/office_docs/docx_01.docx relative to project root)",)+ parser.add_argument("--output-images", help="directory to write image outputs", default="./output_images")args = parser.parse_args()docx_path = Path(args.docx)from mineru.data.data_reader_writer import FileBasedDataWriter- with open(docx_path, 'rb') as f:+ with open(docx_path, "rb") as f:file_bytes = f.read()image_writer = FileBasedDataWriter(args.output_images)middle_json, results = office_docx_analyze(@@ -67,4 +59,5 @@ if __name__ == '__main__':)import json+logger.info(json.dumps(middle_json, indent=2, ensure_ascii=False))diff --git a/mineru/backend/office/model_output_to_middle_json.py b/mineru/backend/office/model_output_to_middle_json.pyindex 2b76cd49..0fcd899e 100644--- a/mineru/backend/office/model_output_to_middle_json.py+++ b/mineru/backend/office/model_output_to_middle_json.py@@ -3,8 +3,8 @@ import refrom collections import defaultdictfrom typing import Any-from ...utils.enum_class import BlockTypefrom ...types import PageInfo+from ...utils.enum_class import BlockTypefrom ...version import __version__from ..utils.html_image_utils import replace_inline_table_images, save_span_image_if_neededfrom .office_magic_model import MagicModel@@ -64,7 +64,6 @@ def blocks_to_page_info(page_blocks: list, image_writer: Any, page_index: int) -# 对page_blocks根据index的值进行排序page_blocks.sort(key=lambda x: x["index"])-page_info = PageInfo(para_blocks=page_blocks,discarded_blocks=discarded_blocks,diff --git a/mineru/backend/office/office_magic_model.py b/mineru/backend/office/office_magic_model.pyindex 4a4b9cd8..02358579 100644--- a/mineru/backend/office/office_magic_model.py+++ b/mineru/backend/office/office_magic_model.py@@ -12,7 +12,7 @@ from mineru.utils.magic_model_utils import tie_up_category_by_indexclass MagicModel:- def __init__(self, page_blocks: list):+ def __init__(self, page_blocks: list) -> None:self.page_blocks = page_blocksblocks = []@@ -143,31 +143,31 @@ class MagicModel:self.text_blocks.append(block)- def get_list_blocks(self):+ def get_list_blocks(self) -> list[Block]:return self.list_blocks- def get_index_blocks(self):+ def get_index_blocks(self) -> list[Block]:return self.index_blocks- def get_image_blocks(self):+ def get_image_blocks(self) -> list[Block]:return self.image_blocks- def get_table_blocks(self):+ def get_table_blocks(self) -> list[Block]:return self.table_blocks- def get_chart_blocks(self):+ def get_chart_blocks(self) -> list[Block]:return self.chart_blocks- def get_title_blocks(self):+ def get_title_blocks(self) -> list[Block]:return self.title_blocks- def get_text_blocks(self):+ def get_text_blocks(self) -> list[Block]:return self.text_blocks- def get_interline_equation_blocks(self):+ def get_interline_equation_blocks(self) -> list[Block]:return self.interline_equation_blocks- def get_discarded_blocks(self):+ def get_discarded_blocks(self) -> list[Block]:return self.discarded_blocksdiff --git a/mineru/backend/office/pptx_analyze.py b/mineru/backend/office/pptx_analyze.pyindex 22cac4f0..1e017c32 100644--- a/mineru/backend/office/pptx_analyze.py+++ b/mineru/backend/office/pptx_analyze.py@@ -3,12 +3,12 @@ import timefrom io import BytesIOfrom loguru import logger-from mineru.backend.office.model_output_to_middle_json import result_to_middle_json-from mineru.model.pptx.main import convert_binary+from ...model.pptx.main import convert_binary+from .model_output_to_middle_json import result_to_middle_json-def office_pptx_analyze(file_bytes, image_writer=None):+def office_pptx_analyze(file_bytes: bytes, image_writer: Any = None) -> dict:infer_start = time.time()file_stream = BytesIO(file_bytes)@@ -16,9 +16,7 @@ def office_pptx_analyze(file_bytes, image_writer=None):infer_time = round(time.time() - infer_start, 2)safe_time = max(infer_time, 0.01)- logger.debug(- f"infer finished, cost: {infer_time}, speed: {round(len(results) / safe_time, 3)} page/s"- )+ logger.debug(f"infer finished, cost: {infer_time}, speed: {round(len(results) / safe_time, 3)} page/s")middle_json = result_to_middle_json(results,@@ -33,15 +31,13 @@ if __name__ == "__main__":# works no matter what the current working directory is when the# module is executed. Allow the user to override the path via a# command-line argument for even greater flexibility.- from pathlib import Pathimport argparse+ from pathlib import Pathscript_root = Path(__file__).resolve().parent.parent.parent.parentdefault_pptx = script_root / "demo" / "office_docs" / "pptx_01.pptx"- parser = argparse.ArgumentParser(- description="Quick demo runner for office_pptx_analyze"- )+ parser = argparse.ArgumentParser(description="Quick demo runner for office_pptx_analyze")parser.add_argument("pptx",nargs="?",diff --git a/mineru/backend/office/xlsx_analyze.py b/mineru/backend/office/xlsx_analyze.pyindex 3aae75d7..476feb63 100644--- a/mineru/backend/office/xlsx_analyze.py+++ b/mineru/backend/office/xlsx_analyze.py@@ -3,12 +3,12 @@ import timefrom io import BytesIOfrom loguru import logger-from mineru.backend.office.model_output_to_middle_json import result_to_middle_json-from mineru.model.xlsx.main import convert_binary+from ...model.xlsx.main import convert_binary+from .model_output_to_middle_json import result_to_middle_json-def office_xlsx_analyze(file_bytes, image_writer=None):+def office_xlsx_analyze(file_bytes: bytes, image_writer: Any = None) -> dict:infer_start = time.time()file_stream = BytesIO(file_bytes)@@ -16,9 +16,7 @@ def office_xlsx_analyze(file_bytes, image_writer=None):infer_time = round(time.time() - infer_start, 2)safe_time = max(infer_time, 0.01)- logger.debug(- f"infer finished, cost: {infer_time}, speed: {round(len(results) / safe_time, 3)} page/s"- )+ logger.debug(f"infer finished, cost: {infer_time}, speed: {round(len(results) / safe_time, 3)} page/s")middle_json = result_to_middle_json(results,@@ -33,15 +31,13 @@ if __name__ == "__main__":# works no matter what the current working directory is when the# module is executed. Allow the user to override the path via a# command-line argument for even greater flexibility.- from pathlib import Pathimport argparse+ from pathlib import Pathscript_root = Path(__file__).resolve().parent.parent.parent.parentdefault_xlsx = script_root / "demo" / "office_docs" / "xlsx_01.xlsx"- parser = argparse.ArgumentParser(- description="Quick demo runner for office_xlsx_analyze"- )+ parser = argparse.ArgumentParser(description="Quick demo runner for office_xlsx_analyze")parser.add_argument("xlsx",nargs="?",