Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

feat: refactor backend structure to separate PDF processing from hybrid components

myhlolie9820b9e0577
diff --git a/mineru/model/flash/docx/docx_converter.py b/mineru/model/flash/docx/docx_converter.py
index de45ae27..38f93a32 100644
--- a/mineru/model/flash/docx/docx_converter.py
+++ b/mineru/model/flash/docx/docx_converter.py
@@ -1351,7 +1351,7 @@ class DocxConverter:
content_text = self._build_text_with_equations_and_hyperlinks(paragraph_elements, text, equations)
if content_text:
title_block = {
- "type": BlockType.TITLE,
+ "type": BlockType.PARAGRAPH_TITLE,
"level": ilevel + 1,
"is_numbered_style": is_numbered,
"content": content_text,
@@ -1381,9 +1381,7 @@ class DocxConverter:
content_text = self._build_text_with_equations_and_hyperlinks(paragraph_elements, text, equations)
if content_text != "":
title_block = {
- "type": BlockType.TITLE,
- "level": 1,
- "is_numbered_style": False,
+ "type": BlockType.DOC_TITLE,
"content": content_text,
}
if paragraph_anchor:
@@ -1396,7 +1394,7 @@ class DocxConverter:
content_text = self._build_text_with_equations_and_hyperlinks(paragraph_elements, text, equations)
if content_text != "":
h_block = {
- "type": BlockType.TITLE,
+ "type": BlockType.PARAGRAPH_TITLE,
"level": p_level if p_level is not None else 2,
"is_numbered_style": is_numbered_style,
"content": content_text,
diff --git a/mineru/model/flash/pptx/pptx_converter.py b/mineru/model/flash/pptx/pptx_converter.py
index 236d542b..33426eb5 100644
--- a/mineru/model/flash/pptx/pptx_converter.py
+++ b/mineru/model/flash/pptx/pptx_converter.py
@@ -41,6 +41,16 @@ A14_DRAWING_NS: Final = "http://schemas.microsoft.com/office/drawing/2010/main"
OMML_NS: Final = "http://schemas.openxmlformats.org/officeDocument/2006/math"
_EFFECTIVE_FONT_SIZE_KEY: Final = "_effective_font_size_pt"
_EFFECTIVE_ALL_BOLD_KEY: Final = "_effective_all_bold"
+# PPTX 标题占位符角色仅用于单页内部归一化,返回 model_output 前必须清理。
+_PPTX_TITLE_ROLE_KEY: Final = "_pptx_title_role"
+_PPTX_TITLE_ROLE_CENTER: Final = "center_title"
+_PPTX_TITLE_ROLE_TITLE: Final = "title"
+_PPTX_TITLE_ROLE_SUBTITLE: Final = "subtitle"
+_PPTX_TITLE_PLACEHOLDER_ROLES: Final = {
+ PP_PLACEHOLDER.CENTER_TITLE: _PPTX_TITLE_ROLE_CENTER,
+ PP_PLACEHOLDER.TITLE: _PPTX_TITLE_ROLE_TITLE,
+ PP_PLACEHOLDER.SUBTITLE: _PPTX_TITLE_ROLE_SUBTITLE,
+}
@dataclass(frozen=True)
@@ -156,6 +166,7 @@ class PptxConverter:
def _walk_linear(self, pptx_obj: presentation.Presentation):
slide_width = int(pptx_obj.slide_width)
slide_height = int(pptx_obj.slide_height)
+ has_visible_content_slide = False
# 遍历每一张幻灯片
for _, slide in enumerate(pptx_obj.slides):
@@ -195,9 +206,16 @@ class PptxConverter:
self.cur_page.extend(entry["blocks"])
self.cur_page.extend(tail_blocks)
+ visible_slide_has_content = bool(self.cur_page)
self._handle_slide_notes(slide)
self._promote_slide_text_blocks_to_titles(self.cur_page)
+ self._finalize_slide_title_types(
+ self.cur_page,
+ is_first_visible_slide=visible_slide_has_content and not has_visible_content_slide,
+ )
self._cleanup_slide_text_block_metadata(self.cur_page)
+ if visible_slide_has_content:
+ has_visible_content_slide = True
self.cur_page = []
self.pages.append(self.cur_page)
@@ -1757,11 +1775,37 @@ class PptxConverter:
block["type"] = BlockType.TITLE
block["level"] = 3
+ @staticmethod
+ def _finalize_slide_title_types(
+ slide_blocks: list[dict],
+ *,
+ is_first_visible_slide: bool,
+ ) -> None:
+ """将 PPTX 内部 title 统一拆分为文档标题、段落标题或普通文本。"""
+ for block in slide_blocks:
+ title_role = block.pop(_PPTX_TITLE_ROLE_KEY, None)
+ if title_role == _PPTX_TITLE_ROLE_SUBTITLE:
+ block["type"] = BlockType.TEXT
+ block.pop("level", None)
+ block.pop("is_numbered_style", None)
+ continue
+
+ if block.get("type") != BlockType.TITLE:
+ continue
+
+ if title_role == _PPTX_TITLE_ROLE_CENTER and is_first_visible_slide:
+ block["type"] = BlockType.DOC_TITLE
+ block.pop("level", None)
+ block.pop("is_numbered_style", None)
+ else:
+ block["type"] = BlockType.PARAGRAPH_TITLE
+
@staticmethod
def _cleanup_slide_text_block_metadata(slide_blocks: list[dict]) -> None:
for block in slide_blocks:
block.pop(_EFFECTIVE_FONT_SIZE_KEY, None)
block.pop(_EFFECTIVE_ALL_BOLD_KEY, None)
+ block.pop(_PPTX_TITLE_ROLE_KEY, None)
def _handle_text_elements(self, shape):
self.list_block_stack = []
@@ -1807,13 +1851,11 @@ class PptxConverter:
}
if shape.is_placeholder:
placeholder_type = shape.placeholder_format.type
- if placeholder_type in [
- PP_PLACEHOLDER.CENTER_TITLE,
- PP_PLACEHOLDER.TITLE,
- PP_PLACEHOLDER.SUBTITLE,
- ]:
+ title_role = _PPTX_TITLE_PLACEHOLDER_ROLES.get(placeholder_type)
+ if title_role is not None:
block["type"] = BlockType.TITLE
block["level"] = 2
+ block[_PPTX_TITLE_ROLE_KEY] = title_role
self.cur_page.append(block)
diff --git a/mineru/model/flash/xlsx/xlsx_converter.py b/mineru/model/flash/xlsx/xlsx_converter.py
index 49b1025c..7eee1394 100644
--- a/mineru/model/flash/xlsx/xlsx_converter.py
+++ b/mineru/model/flash/xlsx/xlsx_converter.py
@@ -266,7 +266,7 @@ class XlsxConverter:
def _build_sheet_title_block(sheet_title: str) -> dict:
"""构造工作表标题块,复用 Office 标题渲染链路输出 Markdown 标题。"""
return {
- "type": BlockType.TITLE,
+ "type": BlockType.PARAGRAPH_TITLE,
"content": sheet_title,
}
diff --git a/tests/unittest/test_flash_office_title_types.py b/tests/unittest/test_flash_office_title_types.py
new file mode 100644
index 00000000..1881283e
--- /dev/null
+++ b/tests/unittest/test_flash_office_title_types.py
@@ -0,0 +1,205 @@
+from __future__ import annotations
+
+from io import BytesIO
+from pathlib import Path
+from typing import Any
+
+from pptx import Presentation
+from pptx.enum.shapes import PP_PLACEHOLDER
+
+from mineru.model.flash import DocxModel, PptxModel, XlsxModel
+from mineru.model.flash.pptx.pptx_converter import (
+ PptxConverter,
+ _PPTX_TITLE_ROLE_CENTER,
+ _PPTX_TITLE_ROLE_KEY,
+ _PPTX_TITLE_ROLE_SUBTITLE,
+ _PPTX_TITLE_ROLE_TITLE,
+)
+from mineru.types import BlockType
+
+
+_PROJECT_ROOT = Path(__file__).resolve().parents[2]
+_OFFICE_SAMPLE_DIR = _PROJECT_ROOT / "demo" / "office_docs"
+
+
+def _predict_sample(model: Any, suffix: str) -> list[list[dict[str, Any]]]:
+ """调用指定 Office model 解析仓库中的真实样例。"""
+ with (_OFFICE_SAMPLE_DIR / f"{suffix}_01.{suffix}").open("rb") as stream:
+ return model.predict(stream)
+
+
+def _flatten_pages(pages: list[list[dict[str, Any]]]) -> list[dict[str, Any]]:
+ """按页面顺序展开 model_output blocks。"""
+ return [block for page in pages for block in page]
+
+
+def _find_block_by_content(blocks: list[dict[str, Any]], content: str) -> dict[str, Any]:
+ """按完整 content 查找唯一 block。"""
+ matches = [block for block in blocks if block.get("content") == content]
+ assert len(matches) == 1
+ return matches[0]
+
+
+def _find_block_containing(blocks: list[dict[str, Any]], content: str) -> dict[str, Any]:
+ """按 content 子串查找唯一 block。"""
+ matches = [block for block in blocks if isinstance(block.get("content"), str) and content in block["content"]]
+ assert len(matches) == 1
+ return matches[0]
+
+
+def _set_subtitle(slide: Any, text: str) -> None:
+ """设置标题页的 Subtitle 占位符文本。"""
+ subtitle = next(shape for shape in slide.placeholders if shape.placeholder_format.type == PP_PLACEHOLDER.SUBTITLE)
+ subtitle.text = text
+
+
+def test_docx_model_splits_document_and_paragraph_titles() -> None:
+ """验证 DOCX model 按样式来源拆分文档标题和段落标题。"""
+ pages = _predict_sample(DocxModel(), "docx")
+ blocks = _flatten_pages(pages)
+
+ assert all(block.get("type") != BlockType.TITLE for block in blocks)
+ assert pages[0][0]["content"] == "MinerU supports DOCX document parsing now"
+
+ doc_title = _find_block_by_content(blocks, "MinerU supports DOCX document parsing now")
+ assert doc_title["type"] == BlockType.DOC_TITLE
+ assert set(doc_title) <= {"type", "content", "anchor"}
+ assert "level" not in doc_title
+ assert "is_numbered_style" not in doc_title
+
+ heading = _find_block_by_content(blocks, "后面是正常的章节标题")
+ assert heading["type"] == BlockType.PARAGRAPH_TITLE
+ assert heading["level"] == 1
+ assert heading["is_numbered_style"] is False
+
+ numbered_heading = _find_block_containing(blocks, "有序列表构成的章节标题")
+ assert numbered_heading["type"] == BlockType.PARAGRAPH_TITLE
+ assert numbered_heading["level"] == 1
+ assert numbered_heading["is_numbered_style"] is True
+
+
+def test_pptx_model_splits_title_placeholders_and_subtitle() -> None:
+ """验证 PPTX 真实样例的首页主标题、Subtitle 和普通页标题映射。"""
+ pages = _predict_sample(PptxModel(), "pptx")
+ blocks = _flatten_pages(pages)
+
+ assert all(block.get("type") != BlockType.TITLE for block in blocks)
+ assert all(_PPTX_TITLE_ROLE_KEY not in block for block in blocks)
+ assert [block["content"] for block in pages[0] if block.get("content") in {"Test Table Slide", "With footnote"}] == [
+ "Test Table Slide",
+ "With footnote",
+ ]
+
+ doc_title = _find_block_by_content(pages[0], "Test Table Slide")
+ assert doc_title["type"] == BlockType.DOC_TITLE
+ assert set(doc_title) <= {"type", "content", "anchor"}
+ assert "level" not in doc_title
+ assert "is_numbered_style" not in doc_title
+
+ subtitle = _find_block_by_content(pages[0], "With footnote")
+ assert subtitle["type"] == BlockType.TEXT
+ assert "level" not in subtitle
+
+ slide_title = _find_block_by_content(pages[1], "Second slide title")
+ assert slide_title["type"] == BlockType.PARAGRAPH_TITLE
+ assert slide_title["level"] == 2
+
+
+def test_pptx_internal_titles_are_finalized_without_losing_levels() -> None:
+ """验证 PPTX 内部 title 收口时清理文档标题字段并保留段落标题层级。"""
+ blocks = [
+ {
+ "type": BlockType.TITLE,
+ "content": "document",
+ "level": 2,
+ "is_numbered_style": False,
+ _PPTX_TITLE_ROLE_KEY: _PPTX_TITLE_ROLE_CENTER,
+ },
+ {
+ "type": BlockType.TITLE,
+ "content": "subtitle",
+ "level": 2,
+ _PPTX_TITLE_ROLE_KEY: _PPTX_TITLE_ROLE_SUBTITLE,
+ },
+ {
+ "type": BlockType.TITLE,
+ "content": "slide title",
+ "level": 2,
+ _PPTX_TITLE_ROLE_KEY: _PPTX_TITLE_ROLE_TITLE,
+ },
+ {
+ "type": BlockType.TITLE,
+ "content": "promoted title",
+ "level": 3,
+ },
+ ]
+
+ PptxConverter._finalize_slide_title_types(blocks, is_first_visible_slide=True)
+
+ assert [block["type"] for block in blocks] == [
+ BlockType.DOC_TITLE,
+ BlockType.TEXT,
+ BlockType.PARAGRAPH_TITLE,
+ BlockType.PARAGRAPH_TITLE,
+ ]
+ assert "level" not in blocks[0]
+ assert "is_numbered_style" not in blocks[0]
+ assert "level" not in blocks[1]
+ assert blocks[2]["level"] == 2
+ assert blocks[3]["level"] == 3
+ assert all(_PPTX_TITLE_ROLE_KEY not in block for block in blocks)
+
+ later_center_title = [
+ {
+ "type": BlockType.TITLE,
+ "content": "later center title",
+ "level": 2,
+ _PPTX_TITLE_ROLE_KEY: _PPTX_TITLE_ROLE_CENTER,
+ }
+ ]
+ PptxConverter._finalize_slide_title_types(
+ later_center_title,
+ is_first_visible_slide=False,
+ )
+ assert later_center_title[0]["type"] == BlockType.PARAGRAPH_TITLE
+ assert later_center_title[0]["level"] == 2
+
+
+def test_pptx_notes_only_slide_does_not_consume_document_title() -> None:
+ """验证空白且仅含备注的前置页不会抢占首个可见文档标题。"""
+ presentation = Presentation()
+ notes_only_slide = presentation.slides.add_slide(presentation.slide_layouts[6])
+ notes_only_slide.notes_slide.notes_text_frame.text = "Presenter note"
+
+ title_slide = presentation.slides.add_slide(presentation.slide_layouts[0])
+ title_slide.shapes.title.text = "Document title"
+ _set_subtitle(title_slide, "Document subtitle")
+
+ later_title_slide = presentation.slides.add_slide(presentation.slide_layouts[0])
+ later_title_slide.shapes.title.text = "Section cover"
+
+ stream = BytesIO()
+ presentation.save(stream)
+ stream.seek(0)
+ pages = PptxModel().predict(stream)
+ blocks = _flatten_pages(pages)
+
+ assert _find_block_by_content(blocks, "Presenter note")["type"] == BlockType.PAGE_FOOTNOTE
+ assert _find_block_by_content(blocks, "Document title")["type"] == BlockType.DOC_TITLE
+ assert _find_block_by_content(blocks, "Document subtitle")["type"] == BlockType.TEXT
+ later_title = _find_block_by_content(blocks, "Section cover")
+ assert later_title["type"] == BlockType.PARAGRAPH_TITLE
+ assert later_title["level"] == 2
+ assert all(block.get("type") != BlockType.TITLE for block in blocks)
+
+
+def test_xlsx_model_emits_sheet_names_as_paragraph_titles() -> None:
+ """验证 XLSX 多 Sheet 名称全部输出为无 level 的段落标题。"""
+ pages = _predict_sample(XlsxModel(), "xlsx")
+ blocks = _flatten_pages(pages)
+ sheet_titles = [_find_block_by_content(blocks, sheet_name) for sheet_name in ("Sheet1", "Sheet2", "Sheet3")]
+
+ assert [page[0]["content"] for page in pages] == ["Sheet1", "Sheet2", "Sheet3"]
+ assert all(block["type"] == BlockType.PARAGRAPH_TITLE for block in sheet_titles)
+ assert all("level" not in block for block in sheet_titles)
+ assert all(block.get("type") not in {BlockType.TITLE, BlockType.DOC_TITLE} for block in blocks)