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: enhance paragraph text extraction to include inline content controls
myhloli69c39f9be291
diff --git a/demo/docx/demo1.docx b/demo/docx/demo1.docxindex 88755b1e..2369ea1e 100644Binary files a/demo/docx/demo1.docx and b/demo/docx/demo1.docx differdiff --git a/mineru/model/docx/docx_converter.py b/mineru/model/docx/docx_converter.pyindex 4860e665..e0d86ae0 100644--- a/mineru/model/docx/docx_converter.py+++ b/mineru/model/docx/docx_converter.py@@ -2,7 +2,7 @@ import reimport zipfilefrom io import BytesIOfrom pathlib import Path-from typing import BinaryIO, Optional, Union, Any, Final+from typing import BinaryIO, Optional, Union, Any, Final, Iteratorimport pandas as pdfrom PIL import Image, ImageDraw, ImageFont@@ -38,6 +38,15 @@ class DocxConverter:"a14": "http://schemas.microsoft.com/office/drawing/2010/main","c": "http://schemas.openxmlformats.org/drawingml/2006/chart",}+ _PARAGRAPH_TRANSPARENT_INLINE_CONTAINERS: Final = {+ "bdo",+ "customXml",+ "dir",+ "fldSimple",+ "ins",+ "moveTo",+ "smartTag",+ }"""Word 文档中使用的 XML 命名空间映射。@@ -1025,9 +1034,10 @@ class DocxConverter:is_section_end = Trueparagraph = Paragraph(element, self.docx_obj)paragraph_elements = self._get_paragraph_elements(paragraph)+ paragraph_text = self._get_paragraph_text(paragraph)paragraph_anchor = self._extract_paragraph_bookmark(element)text, equations = self._handle_equations_in_text(- element=element, text=paragraph.text+ element=element, text=paragraph_text)if text is None:@@ -1135,7 +1145,7 @@ class DocxConverter:self.cur_page.append(h_block)elif len(equations) > 0:- if (paragraph.text is None or len(paragraph.text.strip()) == 0) and len(+ if (paragraph_text is None or len(paragraph_text.strip()) == 0) and len(text) > 0:# 独立公式@@ -1304,14 +1314,17 @@ class DocxConverter:段落元素列表,每个元素包含文本、格式和超链接信息"""+ inner_contents = list(self._iter_paragraph_inner_content(paragraph))+ paragraph_text = self._get_paragraph_text_from_contents(inner_contents)+# 目前保留空段落以保持向后兼容性:- if paragraph.text.strip() == "":+ if paragraph_text.strip() == "":# 检查是否存在带可见样式(下划线或删除线)的空白文本 run。# 有可见样式的空白文本(如带下划线的空格)在视觉上是可见的,应予保留,# 因此跳过提前返回,交由后续完整 run 处理流程处理。has_visible_style_run = any(isinstance(c, Run) and c.text and self._has_visible_style(self._get_format_from_run(c))- for c in paragraph.iter_inner_content()+ for c in inner_contents)if not has_visible_style_run:return [("", None, None)]@@ -1331,7 +1344,7 @@ class DocxConverter:_field_acc_format = None # 首个显示 run 的格式# 遍历段落的 runs 并按格式分组- for c in paragraph.iter_inner_content():+ for c in inner_contents:if isinstance(c, Hyperlink):# 若地址为 URL(含 ://),直接保留字符串,避免 Path 将 // 规范化为 /address = c.address@@ -1466,6 +1479,51 @@ class DocxConverter:return paragraph_elements+ def _iter_paragraph_inner_content(+ self,+ paragraph: Paragraph,+ container: Optional[BaseOxmlElement] = None,+ ) -> Iterator[Union[Run, Hyperlink]]:+ """Yield visible paragraph inline containers in document order.++ python-docx only walks direct ``w:r`` and ``w:hyperlink`` children of ``w:p``.+ Inline ``w:sdt`` content controls are skipped entirely, which drops their text+ from both ``paragraph.text`` and ``paragraph.iter_inner_content()``. This walker+ treats ``w:sdt`` and a few transparent wrapper nodes as pass-through containers+ and reuses the existing Run/Hyperlink wrappers for the actual visible content.+ """+ if container is None:+ container = paragraph._element++ _W_NS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"++ for child in container:+ tag_name = etree.QName(child).localname++ if tag_name == "r":+ yield Run(child, paragraph)+ elif tag_name == "hyperlink":+ yield Hyperlink(child, paragraph)+ elif tag_name == "sdt":+ sdt_content = child.find(f"{{{_W_NS}}}sdtContent")+ if sdt_content is not None:+ yield from self._iter_paragraph_inner_content(paragraph, sdt_content)+ elif tag_name in self._PARAGRAPH_TRANSPARENT_INLINE_CONTAINERS:+ yield from self._iter_paragraph_inner_content(paragraph, child)++ @staticmethod+ def _get_paragraph_text_from_contents(+ inner_contents: list[Union[Run, Hyperlink]],+ ) -> str:+ """Rebuild paragraph plain text from visible inline containers."""+ return "".join(content.text or "" for content in inner_contents)++ def _get_paragraph_text(self, paragraph: Paragraph) -> str:+ """Return paragraph plain text, including inline ``w:sdt`` content."""+ return self._get_paragraph_text_from_contents(+ list(self._iter_paragraph_inner_content(paragraph))+ )+@classmethoddef _resolve_style_chain_bool(cls,@@ -1962,7 +2020,7 @@ class DocxConverter:numid, ilevel = self._get_numId_and_ilvl(paragraph)if numid == 0:numid = None- text = paragraph.text.strip() if paragraph.text else ""+ text = self._get_paragraph_text(paragraph).strip()except Exception:continue@@ -2439,8 +2497,9 @@ class DocxConverter:str: 处理后的文本内容(包含公式标记和超链接格式)"""paragraph_elements = self._get_paragraph_elements(paragraph)+ paragraph_text = self._get_paragraph_text(paragraph)text, equations = self._handle_equations_in_text(- element=paragraph._element, text=paragraph.text+ element=paragraph._element, text=paragraph_text)if text is None:@@ -2635,7 +2694,7 @@ class DocxConverter:for p, position in all_paragraphs:# 创建 Paragraph 对象以获取文本内容paragraph = Paragraph(p, self.docx_obj)- text_content = paragraph.text+ text_content = self._get_paragraph_text(paragraph)# 基于内容和位置创建唯一标识paragraph_id = (text_content, position)