Skip to content

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

feat: enhance markdown table rendering to handle merged cells and fallback for colspan/rowspan

myhlolice8d1c6547ef
diff --git a/mineru/render/markdown.py b/mineru/render/markdown.py
index d48fa0d1..c1d3f06c 100644
--- a/mineru/render/markdown.py
+++ b/mineru/render/markdown.py
@@ -99,9 +99,9 @@ def _build_image_link(block: Block, target_span_type: str, img_bucket_path: str)
def _render_table_block_as_markdown_table(para_block: Block, img_bucket_path: str) -> str:
rendered = merge_visual_para_text(para_block, img_bucket_path)
for span in para_block.all_spans():
- if span.type != ContentType.TABLE or not span.html:
+ if span.type != ContentType.TABLE or not span.content:
continue
- candidate = to_markdown_table(span.html)
- if candidate != span.html.strip():
+ candidate = to_markdown_table(span.content)
+ if candidate != span.content.strip():
return candidate
return rendered
diff --git a/mineru/render/markdown_table.py b/mineru/render/markdown_table.py
index 648bd98f..b35fd456 100644
--- a/mineru/render/markdown_table.py
+++ b/mineru/render/markdown_table.py
@@ -74,6 +74,8 @@ def to_markdown_table(
return raw_html
if _has_nested_table(table):
return raw_html
+ if _has_merged_table_cells(table):
+ return raw_html
rows = table.find_all("tr")
if not rows:
@@ -161,6 +163,14 @@ def _has_nested_table(table: Tag) -> bool:
return table.find("table") is not None
+def _has_merged_table_cells(table: Tag) -> bool:
+ """检测合并单元格,Markdown 表格无法无损表达 rowspan/colspan。"""
+ for cell in table.find_all(("th", "td")):
+ if cell.has_attr("rowspan") or cell.has_attr("colspan"):
+ return True
+ return False
+
+
def _parse_cell(cell: Tag) -> _CellPlacement | None:
if _contains_complex_content(cell):
return None
diff --git a/tests/unittest/test_render_html_table.py b/tests/unittest/test_render_html_table.py
index 1151bf6f..4b32637b 100644
--- a/tests/unittest/test_render_html_table.py
+++ b/tests/unittest/test_render_html_table.py
@@ -40,7 +40,7 @@ def test_to_markdown_table_renders_simple_table() -> None:
)
-def test_to_markdown_table_expands_colspan_cells() -> None:
+def test_to_markdown_table_falls_back_for_colspan_cells() -> None:
html = """
<table>
<tr><th colspan="2">User</th><th>Score</th></tr>
@@ -48,16 +48,10 @@ def test_to_markdown_table_expands_colspan_cells() -> None:
</table>
"""
- assert to_markdown_table(html) == "\n".join(
- [
- "| User | | Score |",
- "| --- | --- | --- |",
- "| Alice | Math | 90 |",
- ]
- )
+ assert to_markdown_table(html) == html.strip()
-def test_to_markdown_table_expands_rowspan_cells() -> None:
+def test_to_markdown_table_falls_back_for_rowspan_cells() -> None:
html = """
<table>
<tr><th>Name</th><th>Subject</th><th>Score</th></tr>
@@ -66,14 +60,7 @@ def test_to_markdown_table_expands_rowspan_cells() -> None:
</table>
"""
- assert to_markdown_table(html) == "\n".join(
- [
- "| Name | Subject | Score |",
- "| --- | --- | --- |",
- "| Alice | Math | 90 |",
- "| | English | 95 |",
- ]
- )
+ assert to_markdown_table(html) == html.strip()
def test_to_markdown_table_preserves_simple_inline_markup() -> None:
@@ -156,6 +143,35 @@ def test_blocks_to_markdown_prefers_markdown_table_when_enabled() -> None:
]
+def test_blocks_to_markdown_keeps_merged_cell_tables_as_html() -> None:
+ html = """
+ <table>
+ <tr><th colspan="2">User</th><th>Score</th></tr>
+ <tr><td>Alice</td><td>Math</td><td>90</td></tr>
+ </table>
+ """.strip()
+ table_block = Block(
+ index=0,
+ type=BlockType.TABLE,
+ bbox=(0.0, 0.0, 10.0, 10.0),
+ blocks=[
+ Block(
+ index=0,
+ type=BlockType.TABLE_BODY,
+ bbox=(0.0, 0.0, 10.0, 10.0),
+ lines=[
+ Line(
+ bbox=(0.0, 0.0, 10.0, 10.0),
+ spans=[Span(type=ContentType.TABLE, bbox=(0.0, 0.0, 10.0, 10.0), content=html)],
+ )
+ ],
+ )
+ ],
+ )
+
+ assert blocks_to_markdown([table_block], prefer_markdown_table=True) == [html]
+
+
def test_render_markdown_prefers_markdown_table_when_enabled() -> None:
html = """
<table>