Skip to content

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

fix: preserve ODF note page ownership

myhloli2a102f852e9d
diff --git a/mineru/model/flash/office/odf/converters.py b/mineru/model/flash/office/odf/converters.py
index 82cdbef5..13513679 100644
--- a/mineru/model/flash/office/odf/converters.py
+++ b/mineru/model/flash/office/odf/converters.py
@@ -11,6 +11,7 @@ from lxml import etree # type: ignore[reportMissingImports]
from .....types import BlockType
from .constants import OdfSuffix, qname
+from .models import InlineNote
from .package import OdfPackage
from .styles import OdfStyles
from .table import parse_table_grid, split_table_regions, table_grid_to_html
@@ -77,7 +78,7 @@ def _flush_notes(parser: OdfBlockParser, page: list[dict[str, Any]]) -> None:
def _append_flow_items(
- items: list[dict[str, Any] | PageBreakMarker],
+ items: list[dict[str, Any] | PageBreakMarker | InlineNote],
*,
parser: OdfBlockParser,
pages: list[list[dict[str, Any]]],
@@ -89,6 +90,8 @@ def _append_flow_items(
if isinstance(item, PageBreakMarker):
_flush_notes(parser, pages[-1])
_new_page(pages, page_masters, current_master)
+ elif isinstance(item, InlineNote):
+ parser.notes.append(item.content)
else:
pages[-1].append(item)
@@ -301,6 +304,7 @@ def _sheet_blocks(sheet: etree._Element, parser: OdfBlockParser) -> list[dict[st
for block in parser.parse_frame_blocks(frame):
if block.get("type") in {BlockType.IMAGE, BlockType.CHART, BlockType.EQUATION}:
blocks.append(block)
+ _flush_notes(parser, blocks)
return blocks
diff --git a/mineru/model/flash/office/odf/models.py b/mineru/model/flash/office/odf/models.py
index 5afc7b14..8d92ac8e 100644
--- a/mineru/model/flash/office/odf/models.py
+++ b/mineru/model/flash/office/odf/models.py
@@ -124,6 +124,13 @@ class InlinePageBreak:
"""表示 ODT 内容中的显式软分页。"""
+@dataclass(frozen=True, slots=True)
+class InlineNote:
+ """保存应随当前行内分页分组归属的 ODF note body。"""
+
+ content: str
+
+
@dataclass(frozen=True, slots=True)
class InlineImage:
"""保存表格单元格中允许内联呈现的图片 data URI。"""
@@ -140,7 +147,15 @@ class InlineBlockGroup:
inline_image_rendered: bool = False
-InlineAtom: TypeAlias = Union[InlineText, InlineMath, InlineBreak, InlinePageBreak, InlineImage, InlineBlockGroup]
+InlineAtom: TypeAlias = Union[
+ InlineText,
+ InlineMath,
+ InlineBreak,
+ InlinePageBreak,
+ InlineNote,
+ InlineImage,
+ InlineBlockGroup,
+]
@dataclass(slots=True)
@@ -179,6 +194,7 @@ __all__ = [
"InlineBreak",
"InlineImage",
"InlineMath",
+ "InlineNote",
"InlinePageBreak",
"InlineText",
"ListLevel",
diff --git a/mineru/model/flash/office/odf/text.py b/mineru/model/flash/office/odf/text.py
index d58bc847..54aab858 100644
--- a/mineru/model/flash/office/odf/text.py
+++ b/mineru/model/flash/office/odf/text.py
@@ -26,6 +26,7 @@ from .models import (
InlineBreak,
InlineImage,
InlineMath,
+ InlineNote,
InlinePageBreak,
InlineText,
TextStyle,
@@ -45,7 +46,7 @@ class PageBreakMarker:
PAGE_BREAK = PageBreakMarker()
-RawFlowItem = dict[str, Any] | PageBreakMarker
+RawFlowItem = dict[str, Any] | PageBreakMarker | InlineNote
def _clean_xml_text(value: str | None) -> str:
@@ -139,7 +140,7 @@ def render_atoms_to_html(atoms: Sequence[InlineAtom]) -> str:
parts.append("<br/>")
elif isinstance(atom, InlineImage):
parts.append(f'<img src="{html.escape(atom.data_uri, quote=True)}" alt="{html.escape(atom.alt, quote=True)}"/>')
- elif isinstance(atom, InlineBlockGroup):
+ elif isinstance(atom, (InlineBlockGroup, InlineNote)):
continue
return "".join(parts)
@@ -173,7 +174,7 @@ def render_atoms_to_model(atoms: Sequence[InlineAtom], *, trim_edges: bool = Fal
parts.append("\n")
elif isinstance(atom, InlineImage) and atom.alt:
parts.append(atom.alt)
- elif isinstance(atom, InlineBlockGroup):
+ elif isinstance(atom, (InlineBlockGroup, InlineNote)):
continue
flush_segments()
return "".join(parts).strip() if trim_edges else "".join(parts)
@@ -318,7 +319,11 @@ class OdfBlockParser:
) -> None:
"""保留脚注标记,并把 note-body 内容排入当前逻辑页脚注队列。"""
citation = note.find(qname("text", "note-citation"))
- citation_text = "".join(citation.itertext()).strip() if citation is not None else str(len(self.notes) + 1)
+ citation_text = (
+ "".join(citation.itertext()).strip()
+ if citation is not None
+ else str(len(self.notes) + sum(isinstance(atom, InlineNote) for atom in atoms) + 1)
+ )
self._append_text_atom(atoms, f"[{citation_text}]", style=style, hyperlink=hyperlink)
body = note.find(qname("text", "note-body"))
if body is None:
@@ -326,7 +331,7 @@ class OdfBlockParser:
blocks = self.parse_container(body)
visible = flatten_block_text(blocks)
if visible:
- self.notes.append(f"[{citation_text}] {visible}")
+ atoms.append(InlineNote(f"[{citation_text}] {visible}"))
def parse_inline_atoms(self, paragraph: etree._Element) -> list[InlineAtom]:
"""解析一个段落的行内语义,并用原位 marker 保留段外 block。"""
@@ -364,7 +369,7 @@ class OdfBlockParser:
is_heading = paragraph.tag == qname("text", "h")
style_name = paragraph.get(qname("text", "style-name"))
for group_index, group in enumerate(groups):
- content_atoms = [atom for atom in group if not isinstance(atom, InlineBlockGroup)]
+ content_atoms = [atom for atom in group if not isinstance(atom, (InlineBlockGroup, InlineNote))]
content = render_atoms_to_model(content_atoms, trim_edges=True)
math_atoms = [atom for atom in content_atoms if isinstance(atom, InlineMath)]
visible_text = "".join(atom.text for atom in content_atoms if isinstance(atom, InlineText)).strip()
@@ -395,6 +400,8 @@ class OdfBlockParser:
for atom in group:
if isinstance(atom, InlineBlockGroup):
results.extend(atom.blocks)
+ elif isinstance(atom, InlineNote):
+ results.append(atom)
if group_index < len(groups) - 1:
results.append(PAGE_BREAK)
return results
@@ -471,6 +478,9 @@ class OdfBlockParser:
continue
if child.tag in {qname("text", "p"), qname("text", "h")}:
for block in self.parse_paragraph(child):
+ if isinstance(block, InlineNote):
+ self.notes.append(block.content)
+ continue
if not isinstance(block, dict):
continue
block_type = block.get("type")
@@ -563,6 +573,9 @@ class OdfBlockParser:
continue
if child.tag == qname("text", "h"):
for parsed in self.parse_paragraph(child):
+ if isinstance(parsed, InlineNote):
+ self.notes.append(parsed.content)
+ continue
if not isinstance(parsed, dict):
continue
if parsed.get("type") == BlockType.PARAGRAPH_TITLE:
@@ -588,6 +601,9 @@ class OdfBlockParser:
if paragraph.tag not in {qname("text", "p"), qname("text", "h")}:
continue
for block in self.parse_paragraph(paragraph):
+ if isinstance(block, InlineNote):
+ self.notes.append(block.content)
+ continue
if isinstance(block, dict) and block.get("content"):
leaves.append({"type": BlockType.TEXT, "content": block["content"]})
if not leaves:
@@ -714,7 +730,13 @@ class OdfBlockParser:
def parse_element(self, element: etree._Element) -> list[dict[str, Any]]:
"""解析一个 ODF block 元素,不移动或修改原始 XML 节点。"""
if element.tag in {qname("text", "p"), qname("text", "h")}:
- return [item for item in self.parse_paragraph(element) if isinstance(item, dict)]
+ blocks: list[dict[str, Any]] = []
+ for item in self.parse_paragraph(element):
+ if isinstance(item, dict):
+ blocks.append(item)
+ elif isinstance(item, InlineNote):
+ self.notes.append(item.content)
+ return blocks
if element.tag == qname("text", "list"):
return self.parse_list_blocks(element)
if element.tag == qname("table", "table"):
@@ -772,6 +794,10 @@ class OdfBlockParser:
elif block.get("image_base64"):
parts.append(f'<img src="{html.escape(str(block["image_base64"]), quote=True)}"/>')
+ def _queue_inline_notes(self, atoms: Sequence[InlineAtom]) -> None:
+ """把单元格行内流中的 note marker 排入当前逻辑页队列。"""
+ self.notes.extend(atom.content for atom in atoms if isinstance(atom, InlineNote))
+
def render_cell_html(self, cell: etree._Element) -> str:
"""把表格单元格中的段落、列表、嵌套表和 frame 转为 HTML。"""
parts: list[str] = []
@@ -780,6 +806,7 @@ class OdfBlockParser:
continue
if child.tag in {qname("text", "p"), qname("text", "h")}:
atoms = self.parse_inline_atoms(child)
+ self._queue_inline_notes(atoms)
rendered_atoms = (
[atom for atom in atoms if not isinstance(atom, InlineImage)] if self._collect_cell_visuals else atoms
)
@@ -821,12 +848,14 @@ class OdfBlockParser:
for child in item:
if child.tag in {qname("text", "p"), qname("text", "h")}:
atoms = self.parse_inline_atoms(child)
+ self._queue_inline_notes(atoms)
parts.append(f"<li>{render_atoms_to_html(atoms)}</li>")
continue
parts.append("<li>")
for child in item:
if child.tag in {qname("text", "p"), qname("text", "h")}:
atoms = self.parse_inline_atoms(child)
+ self._queue_inline_notes(atoms)
parts.append(render_atoms_to_html(atoms))
elif child.tag == qname("text", "list"):
parts.append(self._render_list_html(child, depth=depth + 1, inherited_style=style_name))
diff --git a/tests/unittest/test_flash_odf.py b/tests/unittest/test_flash_odf.py
index 64e3bfd5..cbf1d5b8 100644
--- a/tests/unittest/test_flash_odf.py
+++ b/tests/unittest/test_flash_odf.py
@@ -257,6 +257,45 @@ def test_odf_covered_placeholder_reuses_colspan_coordinate() -> None:
assert [cell.html if cell is not None else None for cell in grid.rows[1]] == ["A", "B", "C"]
+def test_odt_note_after_soft_page_break_stays_on_new_page() -> None:
+ """验证 soft-page-break 后的 note reference 和正文归属同一新页。"""
+ content = """<office:document-content
+ xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
+ xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0">
+ <office:body><office:text><text:p>Before<text:soft-page-break/>After
+ <text:note><text:note-citation>1</text:note-citation><text:note-body><text:p>After note</text:p></text:note-body></text:note>
+ </text:p></office:text></office:body>
+</office:document-content>"""
+
+ pages = OdtModel().predict(BytesIO(build_odf_package("odt", content)))
+
+ assert pages == [
+ [{"type": BlockType.TEXT, "content": "Before"}],
+ [
+ {"type": BlockType.TEXT, "content": "After [1]"},
+ {"type": BlockType.PAGE_FOOTNOTE, "content": "[1] After note"},
+ ],
+ ]
+
+
+def test_ods_cell_note_emits_page_footnote() -> None:
+ """验证 ODS cell citation 对应的 note body 在当前 sheet 页末输出。"""
+ content = """<office:document-content
+ xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
+ xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
+ xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0">
+ <office:body><office:spreadsheet><table:table table:name="Sheet1"><table:table-row><table:table-cell><text:p>Cell
+ <text:note><text:note-citation>1</text:note-citation><text:note-body><text:p>Cell note</text:p></text:note-body></text:note>
+ </text:p></table:table-cell></table:table-row></table:table></office:spreadsheet></office:body>
+</office:document-content>"""
+
+ pages = OdsModel().predict(BytesIO(build_odf_package("ods", content)))
+
+ assert [block["type"] for block in pages[0]] == [BlockType.TABLE, BlockType.PAGE_FOOTNOTE]
+ assert "Cell [1]" in pages[0][0]["content"]
+ assert pages[0][1]["content"] == "[1] Cell note"
+
+
def test_odp_preserves_empty_slide_chart_preview_and_notes() -> None:
"""验证 ODP 空 slide 不丢失,图表同时保留数据和预览,备注归属原页。"""
middle, model = doc_analyze(build_odp_fixture(), file_suffix="odp")