Python docx table addrow() 忽略了我的第三行

Python docx table addrow() ignores my third row

我想用最新版本的 python 开具发票。 为此,我安装并导入了最新版本的 docx 扩展。 我使用此 Word document 作为模板,并想在本文档中间的 table 处添加一行。 带有文本“Gesamtbetrag”的行应该是最后一行。

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

此代码在我的“Gesamtbetrag”行下方添加了一行。 Image of the Error

但它应该看起来像 this。

有什么解决办法吗?

我不知道这个工具是如何工作的,但很明显,您只需向第二个 table 添加一行(tabellen[1] 是 table)。它完全按照你所说的去做——它在 table 的末尾添加了行。也许您必须解决特定的行,例如 tabellen[1].insert_row(1)tabellen[1].add_row(1) 或用于解决要添加的特定行位置的任何内容。

嗯,答案是 add_row() 在 table 的末尾添加一行,而你只有一个 table.

我找到了这个,这可能是您要找的更多内容: