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 functions to improve OOXML suffix guessing from zip packages and file paths
myhlolie92376f570b7
diff --git a/mineru/utils/guess_suffix_or_lang.py b/mineru/utils/guess_suffix_or_lang.pyindex 3a26cbfa..bd838d5c 100644--- a/mineru/utils/guess_suffix_or_lang.py+++ b/mineru/utils/guess_suffix_or_lang.py@@ -1,5 +1,8 @@# Copyright (c) Opendatalab. All rights reserved.+from io import BytesIOfrom pathlib import Path+from zipfile import BadZipFile, ZipFile+from xml.etree import ElementTreefrom loguru import loggerfrom magika import Magika@@ -7,6 +10,32 @@ from magika import MagikaDEFAULT_LANG = "txt"PDF_SIG_BYTES = b'%PDF'+OOXML_ROOT_RELS = "_rels/.rels"+OOXML_CONTENT_TYPES = "[Content_Types].xml"+OOXML_PACKAGE_REL_NS = (+ "http://schemas.openxmlformats.org/package/2006/relationships"+)+OOXML_CONTENT_TYPES_NS = (+ "http://schemas.openxmlformats.org/package/2006/content-types"+)+OOXML_OFFICE_DOCUMENT_REL = (+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/"+ "officeDocument"+)+OOXML_MAIN_CONTENT_TYPES = {+ (+ "application/vnd.openxmlformats-officedocument.wordprocessingml."+ "document.main+xml"+ ): "docx",+ (+ "application/vnd.openxmlformats-officedocument.presentationml."+ "presentation.main+xml"+ ): "pptx",+ (+ "application/vnd.openxmlformats-officedocument.spreadsheetml."+ "sheet.main+xml"+ ): "xlsx",+}magika = Magika()def _normalize_text_for_language_guess(code: str) -> str:@@ -55,7 +84,98 @@ def guess_language_by_text(code):return lang if lang != "unknown" else DEFAULT_LANG+def _strip_package_part_name(part_name: str | None) -> str:+ """规范化 OPC part 路径,方便匹配 Content_Types 中的 PartName。"""+ if not part_name:+ return ""+ return part_name.replace("\\", "/").lstrip("/")+++def _ooxml_relationship_targets(root: ElementTree.Element) -> list[str]:+ """从根关系文件中提取 Office 主文档关系目标。"""+ targets = []+ for relationship in root:+ if relationship.tag not in {+ f"{{{OOXML_PACKAGE_REL_NS}}}Relationship",+ "Relationship",+ }:+ continue+ if relationship.get("TargetMode") == "External":+ continue+ if relationship.get("Type") != OOXML_OFFICE_DOCUMENT_REL:+ continue+ target = _strip_package_part_name(relationship.get("Target"))+ if target:+ targets.append(target)+ return targets+++def _ooxml_content_type_overrides(root: ElementTree.Element) -> dict[str, str]:+ """读取 Content_Types 中每个显式 part 的 ContentType 映射。"""+ overrides = {}+ for override in root:+ if override.tag not in {+ f"{{{OOXML_CONTENT_TYPES_NS}}}Override",+ "Override",+ }:+ continue+ part_name = _strip_package_part_name(override.get("PartName"))+ content_type = override.get("ContentType")+ if part_name and content_type:+ overrides[part_name] = content_type+ return overrides+++def _guess_ooxml_suffix_from_zip(package: ZipFile) -> str | None:+ """根据 OOXML 包内标准主文档关系和主内容类型判断 Office 子类型。"""+ rels_root = ElementTree.fromstring(package.read(OOXML_ROOT_RELS))+ content_types_root = ElementTree.fromstring(package.read(OOXML_CONTENT_TYPES))++ overrides = _ooxml_content_type_overrides(content_types_root)+ for target in _ooxml_relationship_targets(rels_root):+ suffix = OOXML_MAIN_CONTENT_TYPES.get(overrides.get(target, ""))+ if suffix:+ return suffix+ return None+++def _guess_ooxml_suffix_by_bytes(file_bytes: bytes) -> str | None:+ """优先用 OOXML 包结构识别 docx/pptx/xlsx,避免 Magika 被内嵌对象误导。"""+ try:+ with ZipFile(BytesIO(file_bytes)) as package:+ return _guess_ooxml_suffix_from_zip(package)+ except (+ BadZipFile,+ KeyError,+ ElementTree.ParseError,+ RuntimeError,+ OSError,+ ValueError,+ ):+ return None+++def _guess_ooxml_suffix_by_path(file_path: Path) -> str | None:+ """从文件路径读取 OOXML 包结构;失败时交给 Magika 原有逻辑兜底。"""+ try:+ with ZipFile(file_path) as package:+ return _guess_ooxml_suffix_from_zip(package)+ except (+ BadZipFile,+ KeyError,+ ElementTree.ParseError,+ RuntimeError,+ OSError,+ ValueError,+ ):+ return None++def guess_suffix_by_bytes(file_bytes, file_path=None) -> str:+ ooxml_suffix = _guess_ooxml_suffix_by_bytes(file_bytes)+ if ooxml_suffix:+ return ooxml_suffix+suffix = magika.identify_bytes(file_bytes).prediction.output.labelif file_path and suffix in ["ai", "html"] and Path(file_path).suffix.lower() in [".pdf"] and file_bytes[:4] == PDF_SIG_BYTES:suffix = "pdf"@@ -65,6 +185,11 @@ def guess_suffix_by_bytes(file_bytes, file_path=None) -> str:def guess_suffix_by_path(file_path) -> str:if not isinstance(file_path, Path):file_path = Path(file_path)++ ooxml_suffix = _guess_ooxml_suffix_by_path(file_path)+ if ooxml_suffix:+ return ooxml_suffix+suffix = magika.identify_path(file_path).prediction.output.labelif suffix in ["ai", "html"] and file_path.suffix.lower() in [".pdf"]:try: