Search code examples
pythondocxpython-docx

Python docx table addrow() ignores my third row


I would like to write an invoice with the latest version of python. For that I have installed and imported the docx extension in the latest version. I use this Word document as a template and would like to add there a row at the table in the middle of this document. The row with the text "Gesamtbetrag" should be the last row.

    import docx
    doc = docx.Document("Vorlage.docx")
    tabellen = []
    tables = doc.tables
    for table in tables:
        tabellen.append(table)
    row1 = tabellen[1].add_row()

This code adds a row which sits under my "Gesamtbetrag" row. Image of the Error

But it should look like this.

Any ideas how to fix this?


Solution

  • Well, the answer is that add_row() adds a row to the end of the table, and you have only one table.

    I found this, which might be more of what you're looking for: Possible to insert row at specific position with python-docx?