I can't figure out how to delete a table row in python-docx. Specifically, my tables come in with a header row and a row that has a special token stored in the text of the first cell. I search for tables with the token and then fill in a bunch of rows of the table. But how do I delete row 1, which has the token, before adding new rows? I tried
table.rows[1].Delete()
and
table.rows = table.rows[0:1]
The first one fails with an unrecognized function (the documentation refers to this function in the Microsoft API, but I don't know what that means). The second one fails because table.rows is read-only, as the documentation says.
So how do I do it?
This functionality is not built-in, which really shocks me. As I search forums I find many people asking for this over the last five years. However, a workaround exists and here it is:
def remove_row(table, row):
tbl = table._tbl
tr = row._tr
tbl.remove(tr)
row = table.rows[n]
remove_row(table, row)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With