Skip to content

fix(docx): keep the text of wrapped runs in plain table cells - #4455

Open
breken-ai wants to merge 1 commit into
docling-project:mainfrom
breken-ai:fix/docx-table-cell-wrapped-runs
Open

breken-ai wants to merge 1 commit into
docling-project:mainfrom
breken-ai:fix/docx-table-cell-wrapped-runs

Conversation

@breken-ai

Copy link
Copy Markdown

A plain Word table cell loses any text held in an inline content control, a simple field, a smart tag or a tracked insertion.

_handle_tables takes a plain cell's text from python-docx's _Cell.text. That only joins the runs and hyperlinks that are direct children of each paragraph (CT_P.text reads w:r | w:hyperlink). Body paragraphs are read through _iter_paragraph_content, which already goes into w:sdt, w:fldSimple, w:smartTag, w:customXml and w:ins, so the same content survives in a paragraph but is dropped in a table.

This matters most for forms. Word forms are usually tables with the answers in content controls, and mail-merge output keeps its values in MERGEFIELD field results. A cell such as Name | <content control: Jane Doe> comes out as Name | with the answer gone:

label cell content main this PR
Name inline content control (w:sdt) `` Jane Doe
Customer MERGEFIELD result (w:fldSimple) `` ACME Corp
City smart tag `` Paris
Amount tracked insertion (w:ins) `` 1200

Change: a small _get_cell_text helper joins _get_paragraph_text for each cell paragraph with \n, which is the same joining _Cell.text uses. The cell code uses it both for the equation check and for the cell text. Rich cells and everything else are unchanged.

Tests: test_table_cell_keeps_text_of_wrapped_runs builds a table with one wrapper per row. It fails on main ('' != 'Jane Doe') and passes with the fix. tests/test_backend_msword*.py and tests/test_backend_legacy_msoffice.py pass (261 passed, 5 skipped) and no reference data changed. ruff check, ruff format --check and ty check are clean. ty reports the same diagnostics as on main.

I found and fixed this with help from an AI coding assistant, and I reviewed the change and the test results myself.

Checklist:

  • Documentation has been updated, if necessary.
  • Examples have been added, if necessary.
  • Tests have been added, if necessary.

A plain table cell took its text from python-docx's `_Cell.text`, which
only reads runs and hyperlinks that are direct children of a paragraph.
Runs nested in an inline content control, a simple field such as a
mail-merge field, a smart tag or a tracked insertion were dropped, so a
form filled in with content controls came out of a table with empty
answers.

Body paragraphs already read those wrappers through
`_iter_paragraph_content`. Read each cell paragraph the same way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: breken-ai <312387581+breken-ai@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @breken-ai, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🟢 Merge protection satisfied — ready to merge.

Show 1 satisfied protection

🟢 Enforce conventional commit

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\(.+\))?(!)?:

@codecov

codecov Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@ceberam ceberam added bug Something isn't working docx issue related to docx backend labels Oct 1, 2026
@ceberam

ceberam commented Oct 1, 2026

Copy link
Copy Markdown
Member

Thanks @breken-ai for describing the issue and posting this PR!
Before starting the review, could you please attach a sample .docx file (e.g. created with Word or LibreOffice) that shows the cases you described around forms? It would also help comparing the output from main and from your PR.

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

Labels

bug Something isn't working docx issue related to docx backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants