You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Expected behavior
Total width of a table does not change after template rendering and cells become wider. The result should look somewhat like this:
Actual behavior
Actual widths of cells stay the same and, therefore, total width of a table reduces after rendering. The result looks like this:
Possible solution
Currently, my quick-and-dirty workaround is to remove individual width of a cell (<w:tcW>) for the entire document:
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.
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:
fromdocxtplimportDocxTemplateclassDocxTemplateWithWorkaround(DocxTemplate):
# Add the workaround after the original `fix_tables` here:deffix_tables(self, xml):
tree=super().fix_tables(xml)
fortc_wintree.xpath('.//w:tcW', namespaces=tree.nsmap):
tc_w.getparent().remove(tc_w)
returntree# 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")
@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.
Activity
retsyo commentedon Jul 30, 2020
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 commentedon Aug 1, 2020
Hi, @retsyo!
All dynamically generated columns via
{%tc for %}loop will have the same width. Already present columns will stay as is.If I remember correctly, this is the path that I've taken to add this workaround:
cvas22 commentedon Nov 25, 2026
@nikitagashkov @retsyo this is fixed in docform, the maintained fork. When
fix_tablesresizes the grid it now also sets each cell'sw:tcWto 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.