Skip to content

Table width becomes narrower after render #179

Open

Description

@nikitagashkov

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.

Activity

  1. retsyo commented on Jul 30, 2020

    @retsyo

    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?

  2. nikitagashkov commented on Aug 1, 2020

    @nikitagashkov
    ContributorAuthor

    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")
  3. cvas22 commented on Nov 25, 2026

    @cvas22

    @nikitagashkov @retsyo this is fixed in docform, the maintained fork. 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 width after {%tc for %} adds columns (cvas22/docform#206). Thx for the detailed write-up, it made it easy to test.

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