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: update cell merge logic to read metadata from table body and enhance test coverage
myhloli3e0573260be1
diff --git a/mineru/backend/utils/table_merge.py b/mineru/backend/utils/table_merge.pyindex 7f030efc..91428cbd 100644--- a/mineru/backend/utils/table_merge.py+++ b/mineru/backend/utils/table_merge.py@@ -934,8 +934,13 @@ def _apply_cell_merge(cell_merge 按视觉列索引对齐,通过构建视觉列映射来正确匹配两个表格中可能因 rowspan 而具有不同 <td> 元素数量的行。+ 元数据仅从当前页 table body 读取,不依赖外层 table block。"""- cell_merge = current_state.owner_block._cell_merge+ current_body_block = _find_table_body_block(current_state.owner_block)+ if current_body_block is None:+ return++ cell_merge = current_body_block._cell_mergeif not cell_merge:returndiff --git a/mineru/backend/utils/visual_magic_model_utils.py b/mineru/backend/utils/visual_magic_model_utils.pyindex 76159d9b..558da174 100644--- a/mineru/backend/utils/visual_magic_model_utils.py+++ b/mineru/backend/utils/visual_magic_model_utils.py@@ -568,7 +568,6 @@ def regroup_visual_blocks([body_block, *captions, *footnotes],key=_block_index,)- cell_merge = _get_block_field(main_block, "_cell_merge", [])if isinstance(main_block, dict):two_layer_block: BlockLike = {"index": main_index,@@ -579,8 +578,6 @@ def regroup_visual_blocks(two_layer_block["sub_type"] = main_sub_typeif "bbox" in main_block:two_layer_block["bbox"] = deepcopy(main_block["bbox"])- if visual_type == BlockType.TABLE and cell_merge:- two_layer_block["_cell_merge"] = cell_mergeelse:two_layer_block = Block(index=main_index,@@ -590,8 +587,6 @@ def regroup_visual_blocks()if main_sub_type:two_layer_block.sub_type = main_sub_type- if visual_type == BlockType.TABLE and cell_merge:- two_layer_block._cell_merge = cell_mergegrouped_blocks[visual_type].append(two_layer_block)diff --git a/tests/unittest/test_table_merge_cell_merge.py b/tests/unittest/test_table_merge_cell_merge.pynew file mode 100644index 00000000..e3433add--- /dev/null+++ b/tests/unittest/test_table_merge_cell_merge.py@@ -0,0 +1,81 @@+from __future__ import annotations++from mineru.backend.utils.table_merge import _apply_cell_merge, _build_table_state+from mineru.types import Block, BlockType, Line, Span+++def _build_table_block(index: int, html: str, cell_merge: list[int]) -> Block:+ """构造 cell_merge 跨页合并测试使用的两层表格块。"""+ bbox = (0.0, 0.0, 100.0, 100.0)+ body_block = Block(+ index=index,+ type=BlockType.TABLE_BODY,+ bbox=bbox,+ lines=[+ Line(+ bbox=bbox,+ spans=[Span(type=BlockType.TABLE, bbox=bbox, content=html)],+ )+ ],+ )+ body_block._cell_merge = cell_merge+ return Block(+ index=index,+ type=BlockType.TABLE,+ bbox=bbox,+ blocks=[body_block],+ )+++def test_apply_cell_merge_reads_metadata_from_table_body() -> None:+ """验证顶层无 cell_merge 时仍按 table body 元数据续接单元格。"""+ previous_table = _build_table_block(+ 0,+ "<table><tr><td>A</td><td>X</td></tr></table>",+ [],+ )+ current_table = _build_table_block(+ 1,+ "<table><tr><td>B</td><td>Y</td></tr></table>",+ [1, 0],+ )+ previous_state = _build_table_state(previous_table)+ current_state = _build_table_state(current_table)+ assert previous_state is not None+ assert current_state is not None+ assert current_table._cell_merge == []++ _apply_cell_merge(previous_state, current_state, header_count=0)++ previous_cells = previous_state.rows[-1].find_all(["td", "th"])+ current_cells = current_state.rows[0].find_all(["td", "th"])+ assert [cell.get_text() for cell in previous_cells] == ["AB", "X"]+ assert [cell.get_text() for cell in current_cells] == ["", "Y"]+++def test_apply_cell_merge_skips_missing_or_empty_body_metadata() -> None:+ """验证 table body 缺失或 cell_merge 为空时安全跳过。"""+ for remove_body, cell_merge in [(False, []), (True, [1, 0])]:+ previous_table = _build_table_block(+ 0,+ "<table><tr><td>A</td><td>X</td></tr></table>",+ [],+ )+ current_table = _build_table_block(+ 1,+ "<table><tr><td>B</td><td>Y</td></tr></table>",+ cell_merge,+ )+ previous_state = _build_table_state(previous_table)+ current_state = _build_table_state(current_table)+ assert previous_state is not None+ assert current_state is not None+ if remove_body:+ current_table.blocks = []++ _apply_cell_merge(previous_state, current_state, header_count=0)++ previous_cells = previous_state.rows[-1].find_all(["td", "th"])+ current_cells = current_state.rows[0].find_all(["td", "th"])+ assert [cell.get_text() for cell in previous_cells] == ["A", "X"]+ assert [cell.get_text() for cell in current_cells] == ["B", "Y"]diff --git a/tests/unittest/test_visual_magic_model_utils.py b/tests/unittest/test_visual_magic_model_utils.pyindex b878e68a..62550cc6 100644--- a/tests/unittest/test_visual_magic_model_utils.py+++ b/tests/unittest/test_visual_magic_model_utils.py@@ -375,10 +375,32 @@ def test_regroup_visual_blocks_preserves_dict_visual_metadata() -> None:assert "sub_type" not in table_block["content"][0]assert table_block["content"][0]["angle"] == 0assert table_block["content"][0]["score"] == 0.0- assert table_block["_cell_merge"] == [1, 0]+ assert "_cell_merge" not in table_blockassert table_block["content"][0]["_cell_merge"] == [1, 0]+def test_regroup_visual_blocks_keeps_cell_merge_on_block_body() -> None:+ """验证 Block 表格的 cell_merge 只保留在 table body。"""+ table_body = Block(+ index=0,+ type=BlockType.TABLE_BODY,+ bbox=(0.0, 0.0, 80.0, 60.0),+ content="<table></table>",+ )+ table_body._cell_merge = [1, 0]++ grouped_blocks, unmatched_blocks = regroup_visual_blocks([table_body])++ assert unmatched_blocks == []+ table_block = grouped_blocks[BlockType.TABLE][0]+ assert isinstance(table_block, Block)+ assert table_block._cell_merge == []+ assert len(table_block.content) == 1+ grouped_body = table_block.content[0]+ assert isinstance(grouped_body, Block)+ assert grouped_body._cell_merge == [1, 0]++def test_regroup_visual_blocks_without_bbox_prefers_previous_parent() -> None:"""验证无 bbox 等距匹配优先选择 caption 前方的视觉主体。"""image_body = {"index": 0, "type": BlockType.IMAGE_BODY, "content": ""}