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
fix: enhance command-line interface for docx analysis and conversion, allowing user-defined file paths and output directories
myhloli55307e17e1ac
diff --git a/mineru/backend/office/docx_analyze.py b/mineru/backend/office/docx_analyze.pyindex b6addf36..949c4e9b 100644--- a/mineru/backend/office/docx_analyze.py+++ b/mineru/backend/office/docx_analyze.py@@ -29,15 +29,42 @@ def office_docx_analyze(return middle_json, resultsif __name__ == '__main__':- docx_path = "../../../demo/docx/demo1.docx"+ # 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 Path+ import argparse++ script_root = Path(__file__).resolve().parent.parent.parent.parent+ default_docx = script_root / "demo" / "docx" / "demo1.docx"++ 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/docx/demo1.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:file_bytes = f.read()- image_writer = FileBasedDataWriter("./output_images")+ image_writer = FileBasedDataWriter(args.output_images)middle_json, results = office_docx_analyze(file_bytes,image_writer=image_writer,)import json- print(json.dumps(middle_json, indent=2, ensure_ascii=False))\ No newline at end of file+ logger.info(json.dumps(middle_json, indent=2, ensure_ascii=False))\ No newline at end of filediff --git a/mineru/model/docx/main.py b/mineru/model/docx/main.pyindex 47696a71..a582b871 100644--- a/mineru/model/docx/main.py+++ b/mineru/model/docx/main.py@@ -15,4 +15,36 @@ def convert_binary(file_binary: BinaryIO):if __name__ == "__main__":- print(convert_path("../../../demo/docx/demo1.docx"))+ # provide a more robust command-line interface and resolve the demo+ # document path relative to the project root instead of depending on+ # the current working directory.+ from pathlib import Path+ import argparse++ # climb up until we find pyproject.toml or reach a reasonable depth+ def find_project_root(start: Path) -> Path:+ current = start+ for _ in range(6): # avoid infinite loops+ if (current / "pyproject.toml").exists() or (current / "README.md").exists():+ return current+ if current.parent == current:+ break+ current = current.parent+ return start++ script_path = Path(__file__).resolve()+ project_root = find_project_root(script_path.parent)+ default_docx = project_root / "demo" / "docx" / "demo1.docx"++ parser = argparse.ArgumentParser(+ description="Convert a DOCX file to internal JSON representation"+ )+ parser.add_argument(+ "docx",+ nargs="?",+ default=str(default_docx),+ help="path to the .docx file to convert (defaults to demo/docx/demo1.docx)"+ )+ args = parser.parse_args()++ print(convert_path(args.docx))