Repository navigation
Fix table widths: ignore nested rows and update cell widths - #206
Merged
Merged
Conversation
fix_tables() walked every w:tr under a table, so a nested table's rows
counted towards the outer table's column count and its grid got rescaled
for nothing. Only look at the table's own rows now.
When the grid does change, e.g. after {%tc for %} adds columns, each
cell's w:tcW is now set to the width of the grid columns it spans. Word
prefers tcW over the grid, so the old widths left the table narrower.
Fixes #64
Fixes #11
This was referenced Nov 25, 2026
Function fix_tables resets tables configured in Libreoffice Writer
elapouya/python-docx-template#472
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two problems in
fix_tables():t.iter("w:tr"), which includes the rows of tables nested in the cells. A 2-column table holding a 4-column table ended up with 4 grid columns and rescaled widths, even with no tags at all. That's very likely what Function fix_tables resets tables configured in Libreoffice Writer #64 hit with the LibreOffice template. It now only looks at the table's own rows, and nested tables are still fixed on their own pass. Tables without atblGridare skipped instead of raising.w:tcW, and Word uses that over the grid, so after{%tc for %}added columns the table got narrower (Table width becomes narrower after render #11). When the grid is resized, eachdxacell width is now set to the sum of the grid columns it spans. Percentage and auto widths are left alone.Tables whose grid isn't touched come out exactly as before.
The new tests check that a nested table leaves the outer grid alone, and that after adding columns every cell width matches its grid column and the total stays the same. Both fail on main. I checked the XML only, not the rendering in Word or LibreOffice.
Fixes #64
Fixes #11