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.
Activity