Repository navigation
Description
Describe the bug
partition_docx(..., infer_table_structure=True) (the default) silently drops cells from metadata.text_as_html when a DOCX table has a vertically merged cell spanning about 1000 or more rows.
In _convert_table_to_html, the nested iter_row_cells (unstructured/partition/docx.py, line 624) iterates row.cells. In python-docx, row.cells resolves each vMerge="continue" cell by recursing through _tc_above, one stack frame per merged row. With a long merge this raises RecursionError. The except Exception at line 626 catches it, logs a warning, and yields an empty cell, so the rest of those rows is missing from the HTML. No error reaches the caller. The plain Table.text is not affected, because _iter_table_texts walks row._tr.tc_lst and skips vMerge="continue" without recursion.
To Reproduce
import logging
import os
import tempfile
from docx import Document
from docx.oxml.ns import qn
from unstructured.partition.docx import partition_docx
N_ROWS = 1500
with tempfile.TemporaryDirectory() as tmp:
path = os.path.join(tmp, "vmerge.docx")
# first column: one cell merged vertically across all rows; second column: "side-<i>"
doc = Document()
table = doc.add_table(rows=N_ROWS, cols=2)
for i, row in enumerate(table.rows):
row.cells[1].text = f"side-{i}"
tc = row.cells[0]._tc
tcPr = tc.get_or_add_tcPr()
vMerge = tcPr.makeelement(qn("w:vMerge"), {})
if i == 0:
vMerge.set(qn("w:val"), "restart")
row.cells[0].text = "merged-top"
else:
vMerge.set(qn("w:val"), "continue")
tcPr.append(vMerge)
doc.save(path)
warnings = []
class Collect(logging.Handler):
def emit(self, record):
warnings.append(record.getMessage())
logging.getLogger().addHandler(Collect())
elements = partition_docx(filename=path, infer_table_structure=True)
table_element = [e for e in elements if e.category == "Table"][0]
html = table_element.metadata.text_as_html or ""
print("side-1499 in text:", "side-1499" in table_element.text)
print("side-1499 in text_as_html:", "side-1499" in html)
print("warnings:", len(warnings))
print("first warning:", warnings[0])Output:
side-1499 in text: True
side-1499 in text_as_html: False
warnings: 517
first warning: Skipping cell in _convert_table_to_html due to: maximum recursion depth exceeded
Expected behavior
text_as_html contains every row's cell text, as Table.text already does. Truncating a well-formed table because of a Python recursion limit looks unintended.
Environment Info
unstructured0.27.37 (latest on PyPI, same code asmainat 293d235)- python-docx 1.2.0
- Python 3.12.16
- Linux aarch64
Additional context
iter_row_cells is the only place in the HTML-table path that still reads row.cells. The nested-table text and emphasis paths already iterate row._tr.tc_lst and skip vMerge="continue" (changed in #4519). Doing the same in iter_row_cells should avoid the recursion. I'm happy to send a PR.
Activity
fix(docx): stop long vertical merges from dropping table rows from te…