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
{{ message }}
Repository navigation
Bug: Trying to add a table with an absent style creates the table but does not return it #1631
# If the user has passed in a template with proper styles, we# can use the expected table styletry:
table=self.doc.add_table(rows=1, cols=5, style="RDW banded table")
except:
table=self.doc.add_table(rows=1, cols=5)
...
table.rows[0].cells[0].text="Unit"
...
For context, I did this because it was less effort than interrogating the document for the style, so I fully admit I was cheating!
If the style "RDW banded table" is not present in the document (no template passed at Document creation), a KeyError exception is raised. I then catch this and create the table without the style.
The style is not checked in Document.add_table() - the table is created and then its style is set. The exception is then raised when the style is found to be missing, but Document.add_table() does not catch it and remove the table. Because the exception is not handled, it is thrown out to my own calling function; Document.add_table() does not return and I do not have a handle on the table (i.e. the table variable).
I then add the table a second time. At least in LibreOffice Writer, this results in the tables being combined from the GUI point of view, so when I come to populate the first row, the texts are put in the second row of the table from the GUI point of view.
Recommended solutions
Check a style in Document.add_table()before creating the table so that either the exception is thrown out to the caller before a table is created or the table is created anyway, but is returned to the caller.
Create the table in Document.add_table(), but handle the exception when the style is set by deleting the table.
For the optional-template-style case, an application-side workaround is to create the table once, then assign its style separately. That way the missing-style exception does not cause a second add_table() call:
fromdocximportDocumentdefadd_table_with_optional_style(document, rows, cols, style):
table=document.add_table(rows=rows, cols=cols)
try:
table.style=styleexceptKeyError:
pass# Keep the default style; do not create another table.returntabledoc=Document() # Or Document("your-template.docx")table=add_table_with_optional_style(doc, 1, 5, "RDW banded table")
table.cell(0, 0).text="Unit"
This uses the custom style when it exists and keeps the document's default table style when it does not. Catch only KeyError, not a bare except: a wrong style type should still raise ValueError rather than being silently ignored.
I reproduced the original retry pattern with python-docx 1.2.0: it leaves two tables. With the workaround, six focused local tests passed on Python 3.12.3, covering missing/default/built-in/custom styles, propagation of the wrong-type error, and saving/reopening a DOCX. The missing-style round trip contains exactly one table, one row and five columns, with Unit in the first cell. This checks the stored document contents, not Word/LibreOffice GUI rendering.
The upstream API fixes are already proposed in #1632 and #1637; this is a workaround for callers while those are pending, not another competing PR.
AI-assisted support note, posted on the account owner's behalf; the code and saved-file checks were executed locally.
Activity
bactiar291 commentedon Dec 3, 2026
For the optional-template-style case, an application-side workaround is to create the table once, then assign its style separately. That way the missing-style exception does not cause a second
add_table()call:This uses the custom style when it exists and keeps the document's default table style when it does not. Catch only
KeyError, not a bareexcept: a wrong style type should still raiseValueErrorrather than being silently ignored.I reproduced the original retry pattern with python-docx 1.2.0: it leaves two tables. With the workaround, six focused local tests passed on Python 3.12.3, covering missing/default/built-in/custom styles, propagation of the wrong-type error, and saving/reopening a DOCX. The missing-style round trip contains exactly one table, one row and five columns, with
Unitin the first cell. This checks the stored document contents, not Word/LibreOffice GUI rendering.The upstream API fixes are already proposed in #1632 and #1637; this is a workaround for callers while those are pending, not another competing PR.
AI-assisted support note, posted on the account owner's behalf; the code and saved-file checks were executed locally.