Skip to content

fix: keep the text of tables nested in DOCX table cells - #13959

Open
hass44 wants to merge 1 commit into
deepset-ai:mainfrom
hass44:fix/docx-nested-table-in-cell
Open

hass44 wants to merge 1 commit into
deepset-ai:mainfrom
hass44:fix/docx-nested-table-in-cell

Conversation

@hass44

@hass44 hass44 commented Nov 26, 2026

Copy link
Copy Markdown

Related Issues

Proposed Changes:

DOCXToDocument read each table cell through cell.paragraphs, which holds only the cell's direct paragraphs, so a table nested in a cell was dropped without a warning. Nested tables are common in Word forms, invoices and CV templates, where the dropped part is often the content people search for (contact details, line items).

_cell_text now walks the cell with cell.iter_inner_content(). A nested table is written in place, one line per row, with its cells separated by |. It works at any depth, links inside it follow link_format, and a merged cell of the nested table is read once instead of once per row or column it spans.

With a 2x2 table nested in the second cell (table_format="csv"), the output changes from outer, to:

outer,"Name | Asha
Email | [email protected]"

How did you test it?

  • New unit tests in test_docx_file_to_document.py: nested table in both table formats, two levels deep, merged cells in the nested table, a link in a nested cell. All 5 fail on main and pass with the change. The whole module passes (56 tests).
  • ruff check, ruff format --check and mypy on the changed files are clean.

Notes for the reviewer

#13906 fixed the same issue but was closed because its CLA was never signed. This PR also keeps the nested rows on one line each and reads merged cells once.

Checklist

DOCXToDocument read a table cell through cell.paragraphs, which holds only the
cell's direct paragraphs, so a table nested in the cell was dropped. The cell is
now read with iter_inner_content(), and a nested table is written in place, one
line per row with its cells separated by " | ". Merged cells of the nested
table are read once.

Fixes deepset-ai#13905
@hass44
hass44 requested a review from a team as a code owner November 26, 2026 19:42
@hass44
hass44 requested review from anakin87 and removed request for a team November 26, 2026 19:42
@vercel

vercel Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

@hass44 is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant

CLAassistant commented Nov 26, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@ayushmnnit

Copy link
Copy Markdown

Ran this at b0b5f44 against main (45949b5). With python-docx 1.2.0 the fix works: a 1x2 table with a 1x2 table nested in the second cell goes from 'Contact,' to 'Contact,Name | Asha', the 5 new tests fail on main, and the module passes (56).

One problem: _Cell.iter_inner_content() only exists from python-docx 1.1.0, and pyproject.toml lists python-docx with no floor. On 1.0.1 (which main still handles for ordinary tables) every DOCX with any table now fails to convert:

Could not read plain.docx and convert it to a DOCX Document, skipping. Error: '_Cell' object has no attribute 'iter_inner_content'

That includes a plain 1x2 table with no nesting (main returns 'a,b'). The test module on 1.0.1 goes from 5 failed on main (metadata fields and a hand-built text box without w:tblGrid, both unrelated) to 20 failed on this branch.

Two ways out: add python-docx>=1.1.0, or walk the cell's children directly, the same way _extract_elements walks document.element.body:

for child in cell._tc.iterchildren():
    if child.tag.endswith("}p"):
        lines.append(self._process_links_in_paragraph(Paragraph(child, cell)))
    elif child.tag.endswith("}tbl"):
        lines.extend(self._nested_table_lines(Table(child, cell)))

With that swapped in, all 5 new tests pass on both 1.0.1 and 1.2.0, and 1.0.1 is back to main's 5 unrelated failures.

Smaller note, not a regression: a nested table wrapped in a block content control (w:sdt in the cell, common in Word form templates) is still dropped on both main and this branch, since neither cell.paragraphs nor iter_inner_content() enters w:sdt. Fine as a follow-up if it's out of scope here.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DOCXToDocument drops the text of a table nested inside a table cell

3 participants