Repository navigation
Set table grid column widths from cell borders - #378
Open
ferreiradev wants to merge 1 commit into
Open
ferreiradev wants to merge 1 commit into
ferreiradev wants to merge 1 commit into
Conversation
python-docx creates the table grid (w:tblGrid) with equal columns, while pdf2docx sets the real width on each cell only. With fixed layout, editors like MS Word lay out the table by the grid, so columns come out wrong and tables may overflow the page. Cells continuing a vertical merge also kept the default equal width. Derive each grid column width from the left/right borders of the parsed cells and give vertically merged continuation cells the width of the columns they span. The grid is left untouched if any column border is unknown. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Problem
Blocks.make_docxcreates each table withdoc.add_table(...), so python-docx builds the grid (w:tblGrid) with equal column widths and turns off autofit.Cell.make_docxthen sets the real width on each cell (w:tcW), but the grid is never updated. Cells continuing a vertical merge (w:vMergewithoutrestart) don't get a real width either, and keep the default equal width.With fixed layout, MS Word (and other editors, e.g. Pages) lays out the table by the grid, or by a mix of grid and cell widths. The columns come out with the wrong widths, and tables may overflow the page even though every cell width is correct.
Fix
TableBlock._column_widths()derives each grid column width from the left/right borders of the parsed cells. Merged cells are skipped, and a merged cell's right border gives the right border of its last column.docx.set_column_widths()writes those widths tow:gridCol, and gives vertically merged continuation cells the width of the grid columns they span.This is compatible with
python-docx>=0.8.10: it only uses_Column.width,CT_Tc.grid_span,CT_Tc.vMergeandCT_Tc.width.Results
Cells whose
w:tcWdiffers from the grid columns they span (by more than 1pt):demo-table.pdfdemo-table-shading.pdfdemo-table-shading-highlight.pdfdemo-table-empty-cell.pdf(#358)test/samples/demo-table*.pdfanddemo.pdfThese counts are from the 14 table samples plus
demo.pdf. I also checked the rendered output visually (DOCX → PDF) on several documents with merged cells, wide multi-page tables and a label/value table. The tables now keep the PDF column widths and fit the page.Related to #358: the file there is
demo-table-empty-cell.pdf, and its columns now keep the PDF widths. Structure detection issues in that file are unaffected.Tests
Added
TestConversion::test_table_grid_matches_cell_widths. It convertsdemo-table.pdfand checks that every cell spans grid columns as wide as the cell, including vertical-merge continuation cells. It fails on master and passes with this change.pytest test/test.py::TestConversiongives 6 passed.🤖 Generated with Claude Code