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
feat: add support for DOCX file format in converter
myhlolif44fb174eafe
diff --git a/mineru/backend/office/__init__.py b/mineru/backend/office/__init__.pynew file mode 100644index 00000000..1e17167c--- /dev/null+++ b/mineru/backend/office/__init__.py@@ -0,0 +1 @@+# Copyright (c) Opendatalab. All rights reserved.diff --git a/mineru/backend/office/docx.py b/mineru/backend/office/docx.pynew file mode 100644index 00000000..1e17167c--- /dev/null+++ b/mineru/backend/office/docx.py@@ -0,0 +1 @@+# Copyright (c) Opendatalab. All rights reserved.diff --git a/mineru/cli/common.py b/mineru/cli/common.pyindex 23aaeec7..72c0a053 100644--- a/mineru/cli/common.py+++ b/mineru/cli/common.py@@ -28,6 +28,7 @@ if os.getenv("MINERU_LMDEPLOY_DEVICE", "") == "maca":pdf_suffixes = ["pdf"]image_suffixes = ["png", "jpeg", "jp2", "webp", "gif", "bmp", "jpg", "tiff"]+docx_suffixes = ["docx"]os.environ["TOKENIZERS_PARALLELISM"] = "false"@@ -39,7 +40,7 @@ def read_fn(path):file_suffix = guess_suffix_by_bytes(file_bytes, path)if file_suffix in image_suffixes:return images_bytes_to_pdf_bytes(file_bytes)- elif file_suffix in pdf_suffixes:+ elif file_suffix in pdf_suffixes + docx_suffixes:return file_byteselse:raise Exception(f"Unknown file suffix: {file_suffix}")diff --git a/mineru/model/docx/docx_converter.py b/mineru/model/docx/docx_converter.pyindex 45e5486a..02d8ffdf 100644--- a/mineru/model/docx/docx_converter.py+++ b/mineru/model/docx/docx_converter.py@@ -5,7 +5,6 @@ from pathlib import Pathfrom typing import BinaryIO, Optional, Union, Any, Finalimport logging-import filetypefrom PIL import Image, UnidentifiedImageErrorfrom loguru import loggerfrom docx import Document@@ -14,8 +13,6 @@ from docx.text.paragraph import Paragraphfrom docx.text.hyperlink import Hyperlinkfrom docx.text.run import Runfrom lxml import etree-from markdown_it.common.html_re import attribute-from markdown_it.rules_block import list_blockfrom pydantic import AnyUrlfrom mineru.model.utils.docx.mammoth import body_xml@@ -87,23 +84,6 @@ class DocxConverter:self.equation_bookends: str = "<eq>{EQ}</eq>" # 公式标记格式Path.mkdir(self.output_path, parents=True, exist_ok=True)- def accepts(- self,- file_stream: BinaryIO,- ) -> bool:- mimetype = filetype.guess_mime(file_stream).lower()- if mimetype is None:- logger.error(f"Failed to detect mimetype for {self.file_path}")- extension = os.path.splitext(self.file_path)[1].lower()-- if extension in ACCEPTED_FILE_EXTENSIONS:- return True-- for prefix in ACCEPTED_MIME_TYPE_PREFIXES:- if mimetype.startswith(prefix):- return True-- return Falsedef convert(self,diff --git a/mineru/model/docx/main.py b/mineru/model/docx/main.pyindex b795ce18..69a4f4cf 100644--- a/mineru/model/docx/main.py+++ b/mineru/model/docx/main.py@@ -9,8 +9,6 @@ def convert_path(file_path: str):def convert_binary(file_binary: BinaryIO, file_path: str):converter = DocxConverter(file_path=file_path, output_path="./output")- if not converter.accepts(file_binary):- raise Exception("Not a docx file")return converter.convert(file_binary)diff --git a/pyproject.toml b/pyproject.tomlindex 78f5ed33..760b1c13 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -41,10 +41,8 @@ dependencies = ["magika>=0.6.2,<1.1.0","mineru-vl-utils>=0.1.19.1,<1","qwen-vl-utils>=0.0.14,<1",- "mammoth>=1.11.0,<2",- "defusedxml",- "filetype",- "markdownify",+ "defusedxml>=0.7.1,<1",+ "python-docx>=1.2.0,<2",][project.optional-dependencies]