Skip to content

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

fix: bound CSV tables and honor ODF page resets

myhlolifa6d59f6fbde
diff --git a/mineru/model/flash/csv.py b/mineru/model/flash/csv.py
index 11446fc5..810f6d3a 100644
--- a/mineru/model/flash/csv.py
+++ b/mineru/model/flash/csv.py
@@ -18,7 +18,8 @@ from ...types import BlockType
MAX_CSV_BYTES: Final = 200 * 1024 * 1024
MAX_CSV_ROWS: Final = 1_048_576
MAX_CSV_COLUMNS: Final = 16_384
-MAX_CSV_GRID_SLOTS: Final = 4_000_000
+# CSV 会把每个槽位实体化为 HTML/DOM 节点,预算需显著低于稀疏电子表格投影上限。
+MAX_CSV_GRID_SLOTS: Final = 250_000
_DELIMITER_CANDIDATES: Final = (",", ";", "\t", "|")
_DELIMITER_SAMPLE_RECORDS: Final = 20
@@ -255,16 +256,17 @@ def _render_field_html(value: str) -> str:
def _rows_to_html(rows: list[list[str]], *, has_header: bool) -> str:
- """把规则矩形 CSV 网格转换为现有 TableBlock 可消费的安全 HTML。"""
- lines = ["<table>"]
+ """增量构造安全表格 HTML,避免为每个单元格保留独立字符串对象。"""
+ output = StringIO()
+ output.write("<table>")
for row_index, row in enumerate(rows):
tag = "th" if has_header and row_index == 0 else "td"
- lines.append(" <tr>")
+ output.write("\n <tr>")
for value in row:
- lines.append(f" <{tag}>{_render_field_html(value)}</{tag}>")
- lines.append(" </tr>")
- lines.append("</table>")
- return "\n".join(lines)
+ output.write(f"\n <{tag}>{_render_field_html(value)}</{tag}>")
+ output.write("\n </tr>")
+ output.write("\n</table>")
+ return output.getvalue()
def convert_csv(file_binary: BinaryIO) -> list[list[dict[str, Any]]]:
diff --git a/mineru/model/flash/office/odf/models.py b/mineru/model/flash/office/odf/models.py
index 0d81e43d..5afc7b14 100644
--- a/mineru/model/flash/office/odf/models.py
+++ b/mineru/model/flash/office/odf/models.py
@@ -79,6 +79,15 @@ class ParagraphProperties:
master_page_name: str | None = None
+@dataclass(frozen=True, slots=True)
+class ParagraphPropertiesDelta:
+ """保存可区分缺省与显式 reset 的 ODF 段落分页增量。"""
+
+ break_before: bool | None = None
+ break_after: bool | None = None
+ master_page_name: str | None = None
+
+
@dataclass(frozen=True, slots=True)
class ListLevel:
"""保存一个 ODF 列表层级的编号语义。"""
@@ -174,6 +183,7 @@ __all__ = [
"InlineText",
"ListLevel",
"ParagraphProperties",
+ "ParagraphPropertiesDelta",
"TableGrid",
"TextStyle",
"TextStyleDelta",
diff --git a/mineru/model/flash/office/odf/styles.py b/mineru/model/flash/office/odf/styles.py
index 0b900ac1..488c8628 100644
--- a/mineru/model/flash/office/odf/styles.py
+++ b/mineru/model/flash/office/odf/styles.py
@@ -9,7 +9,7 @@ from loguru import logger
from lxml import etree # type: ignore[reportMissingImports]
from .constants import qname
-from .models import ListLevel, ParagraphProperties, TextStyle, TextStyleDelta
+from .models import ListLevel, ParagraphProperties, ParagraphPropertiesDelta, TextStyle, TextStyleDelta
@dataclass(frozen=True, slots=True)
@@ -21,7 +21,7 @@ class _StyleDefinition:
parent: str | None
display_name: str | None
text_delta: TextStyleDelta
- paragraph: ParagraphProperties
+ paragraph: ParagraphPropertiesDelta
table_display: bool | None
@@ -122,14 +122,14 @@ class OdfStyles:
return False
@staticmethod
- def _paragraph_properties(style: etree._Element) -> ParagraphProperties:
- """读取段落样式中的显式分页与 master-page 信息。"""
+ def _paragraph_properties(style: etree._Element) -> ParagraphPropertiesDelta:
+ """读取可区分缺省与显式非分页值的段落属性增量。"""
properties = style.find(qname("style", "paragraph-properties"))
before = properties.get(qname("fo", "break-before")) if properties is not None else None
after = properties.get(qname("fo", "break-after")) if properties is not None else None
- return ParagraphProperties(
- break_before=before == "page",
- break_after=after == "page",
+ return ParagraphPropertiesDelta(
+ break_before=None if before is None else before.casefold() == "page",
+ break_after=None if after is None else after.casefold() == "page",
master_page_name=style.get(qname("style", "master-page-name")),
)
@@ -230,8 +230,10 @@ class OdfStyles:
after = False
master: str | None = None
for definition in reversed(chain):
- before = before or definition.paragraph.break_before
- after = after or definition.paragraph.break_after
+ if definition.paragraph.break_before is not None:
+ before = definition.paragraph.break_before
+ if definition.paragraph.break_after is not None:
+ after = definition.paragraph.break_after
master = definition.paragraph.master_page_name or master
return ParagraphProperties(before, after, master)
diff --git a/tests/unittest/test_flash_csv.py b/tests/unittest/test_flash_csv.py
index 76ebaa0d..1602dadd 100644
--- a/tests/unittest/test_flash_csv.py
+++ b/tests/unittest/test_flash_csv.py
@@ -146,6 +146,16 @@ def test_csv_grid_limit_short_circuits_before_trailing_malformed_record(monkeypa
CsvModel().predict(BytesIO(b'a,b\n1,2\n"unterminated'))
+def test_csv_default_grid_budget_rejects_wide_dom_before_rendering() -> None:
+ """验证默认预算在宽空表生成数十万 HTML 节点前拒绝输入。"""
+ assert csv_module.MAX_CSV_GRID_SLOTS == 250_000
+ wide_empty_row = b"," * (csv_module.MAX_CSV_COLUMNS - 1) + b"\n"
+ payload = wide_empty_row * 16
+
+ with pytest.raises(ValueError, match="max_grid_slots"):
+ CsvModel().predict(BytesIO(payload))
+
+
def test_csv_doc_analyze_sync_async_and_render_contracts_match() -> None:
"""验证同步异步 Analyze 元数据和既有表格渲染链保持一致。"""
payload = "姓名,年龄\n张三,30\n".encode()
diff --git a/tests/unittest/test_flash_odf.py b/tests/unittest/test_flash_odf.py
index aa538bdb..d73e83bf 100644
--- a/tests/unittest/test_flash_odf.py
+++ b/tests/unittest/test_flash_odf.py
@@ -198,6 +198,39 @@ def test_odt_inline_visual_stays_before_soft_page_break() -> None:
]
+def test_odt_child_style_can_reset_inherited_page_breaks() -> None:
+ """验证 break-before/after=auto 显式关闭父样式的逻辑分页。"""
+ 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:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
+ xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0">
+ <office:automatic-styles>
+ <style:style style:name="Parent" style:family="paragraph">
+ <style:paragraph-properties fo:break-before="page" fo:break-after="page"/>
+ </style:style>
+ <style:style style:name="Inherited" style:family="paragraph" style:parent-style-name="Parent"/>
+ <style:style style:name="Reset" style:family="paragraph" style:parent-style-name="Parent">
+ <style:paragraph-properties fo:break-before="auto" fo:break-after="auto"/>
+ </style:style>
+ </office:automatic-styles>
+ <office:body><office:text>
+ <text:p>Before</text:p><text:p text:style-name="Reset">Reset</text:p><text:p>After</text:p>
+ </office:text></office:body>
+</office:document-content>"""
+ inherited_content = content.replace('text:style-name="Reset"', 'text:style-name="Inherited"')
+
+ reset_pages = OdtModel().predict(BytesIO(build_odf_package("odt", content)))
+ inherited_pages = OdtModel().predict(BytesIO(build_odf_package("odt", inherited_content)))
+
+ assert [[block.get("content") for block in page] for page in reset_pages] == [["Before", "Reset", "After"]]
+ assert [[block.get("content") for block in page] for page in inherited_pages] == [
+ ["Before"],
+ ["Reset"],
+ ["After"],
+ ]
+
+
def test_odp_preserves_empty_slide_chart_preview_and_notes() -> None:
"""验证 ODP 空 slide 不丢失,图表同时保留数据和预览,备注归属原页。"""
middle, model = doc_analyze(build_odp_fixture(), file_suffix="odp")