Skip to content

Table width becomes narrower after render #11

Done

Description

@cvas22

Copied from python-docx-template issue 179, opened by @nikitagashkov on 2019-01-25.


The DocxTemplate#fix_tables does not fix width of a table.

Details

The problem is that DocxTemplate#fix_tables correctly updates <w:tblGrid> but does not change <w:tcW> of an individual cell. MS Word prioritizes <w:tcW> over <w:tblGrid> (see this link) and renders cells with same widths.

How to reproduce

Render the template such as the one below and open the output file in MS Word.
tpl

Expected behavior

Total width of a table does not change after template rendering and cells become wider. The result should look somewhat like this:
post-fix

Actual behavior

Actual widths of cells stay the same and, therefore, total width of a table reduces after rendering. The result looks like this:
pre-fix

Possible solution

Currently, my quick-and-dirty workaround is to remove individual width of a cell (<w:tcW>) for the entire document:

for tc_w in tree.xpath('.//w:tcW', namespaces=tree.nsmap):
    tc_w.getparent().remove(tc_w)

Warning! This approach works only for tables that should autofit window (check out this link for details).

I think, this won't work for everyone, so I haven't made a pull request. Probably, the right way is to walk through all cells of a table and update its' individual widths with corresponding <w:gridCol> value.

Source files

  1. template.docx;
  2. pre-change.docx;
  3. post-change.docx.

Comments from the original thread

@retsyo on 2020-07-30:

sorry, but is this the solution to keep rendered table has the same column widths in template docs file? and how can I use this code?

@nikitagashkov on 2020-08-01:

Hi, @retsyo!

this the solution to keep rendered table has the same column widths in template docs file?

All dynamically generated columns via {%tc for %} loop will have the same width. Already present columns will stay as is.

and how can I use this code?

If I remember correctly, this is the path that I've taken to add this workaround:

from docxtpl import DocxTemplate


class DocxTemplateWithWorkaround(DocxTemplate):
    # Add the workaround after the original `fix_tables` here:
    def fix_tables(self, xml):
        tree = super().fix_tables(xml)

        for tc_w in tree.xpath('.//w:tcW', namespaces=tree.nsmap):
            tc_w.getparent().remove(tc_w)

        return tree

# After that, you can use `DocxTemplateWithWorkaround` the same way as you did before:
doc = DocxTemplateWithWorkaround("my_word_template.docx")
context = { 'company_name' : "World company" }
doc.render(context)
doc.save("generated_doc.docx")

Activity

  1. cvas22 commented on Nov 25, 2026

    @cvas22
    OwnerAuthor

    @nikitagashkov thx for the clear write-up. When fix_tables resizes the grid it now also sets each cell's w:tcW to the columns it spans, so the table keeps its total width after {%tc for %} adds columns. Percentage and auto widths are left as they are. Fixed in #206, it'll be in the next release.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions