Skip to content

Bug: Trying to add a table with an absent style creates the table but does not return it #1631

Open

Description

@JackNWhite

Consider this snippet:

        # If the user has passed in a template with proper styles, we
        # can use the expected table style
        try:
            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.

Image

Recommended solutions

  1. 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.
  2. Create the table in Document.add_table(), but handle the exception when the style is set by deleting the table.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions