0

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?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

2 Answers2

2

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?

Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
Alex K
  • 61
  • 6
0

A have no idea how this tool works, but it's obvious that you just add a row to the second table ( tabellen[1] is a table ). And it does exactly what uou said - it adds row to the end of the table. Maybe you have to address a specific row, like tabellen[1].insert_row(1), or tabellen[1].add_row(1) or whatever it uses to address a specific row position to add.

Mechanic
  • 319
  • 3
  • 8