Python-docx: feature: Row.delete()

Created on 5 Aug 2014  路  4Comments  路  Source: python-openxml/python-docx

The Table object has a way to add a row with add_row but no way to remove a row.

table

Most helpful comment

Here is a simple function that can be used to remove a table row...

def remove_row(table, row):
    tbl = table._tbl
    tr = row._tr
    tbl.remove(tr)

All 4 comments

Here is a simple function that can be used to remove a table row...

def remove_row(table, row):
    tbl = table._tbl
    tr = row._tr
    tbl.remove(tr)

there is no column._tc, so ho can we delete a column?

@retsyo Like in HTML, there is no explicit element representing a column. A table is a sequence of rows, each of which is a sequence of cells.

To delete a column n, delete the n-th cell in each row.

Note there is also a a:tblGrid/a:gridCol element for each "grid" column and you'll need to delete the right one of those too.

Also note that there is such a thing as a a:tc element, but it means "table cell" rather than table column.

A little more on these details is here:
http://python-pptx.readthedocs.io/en/latest/dev/analysis/shp-table.html

If there are merged cells in the table that may complicate matters.

So, presumably you just need to add a method to the Row class like this:

def remove(self):
    self._parent._tbl.remove(self._tr)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

madphysicist picture madphysicist  路  3Comments

ronbarak picture ronbarak  路  3Comments

kociak picture kociak  路  5Comments

carlosmtz2106 picture carlosmtz2106  路  7Comments

evbo picture evbo  路  3Comments