Skip to content

Set table grid column widths from cell borders - #378

Open
ferreiradev wants to merge 1 commit into
ArtifexSoftware:masterfrom
ferreiradev:fix/table-grid-column-widths
Open

ferreiradev wants to merge 1 commit into
ArtifexSoftware:masterfrom
ferreiradev:fix/table-grid-column-widths

Conversation

@ferreiradev

Copy link
Copy Markdown

Problem

Blocks.make_docx creates each table with doc.add_table(...), so python-docx builds the grid (w:tblGrid) with equal column widths and turns off autofit. Cell.make_docx then sets the real width on each cell (w:tcW), but the grid is never updated. Cells continuing a vertical merge (w:vMerge without restart) 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 to w:gridCol, and gives vertically merged continuation cells the width of the grid columns they span.
  • If any column border can't be determined, the grid is left as it is (current behaviour).

This is compatible with python-docx>=0.8.10: it only uses _Column.width, CT_Tc.grid_span, CT_Tc.vMerge and CT_Tc.width.

Results

Cells whose w:tcW differs from the grid columns they span (by more than 1pt):

Sample master this PR
demo-table.pdf 19 / 11 / 34 (per table) 0
demo-table-shading.pdf 1 / 345 0
demo-table-shading-highlight.pdf 2 / 259 0
demo-table-empty-cell.pdf (#358) up to 32 per table 0
all other test/samples/demo-table*.pdf and demo.pdf > 0 0

These 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 converts demo-table.pdf and 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::TestConversion gives 6 passed.

🤖 Generated with Claude Code

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant