Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python docx delete table from document

Tags:

python-docx

I want to remove some tables from a document based on the contents of the upper left cell.

I tried:

allTables = document.tables
for activeTable in allTables:
    if activeTable.cell(0,0).paragraphs[0].text == 'some text':
        allTables.remove(activeTable)

I expected to have removed all tables containing 'some text' in cell(0,0), but they are still in the document.

The process enters the line with "allTables.remove(activeTable)" as expected: indexToDelete = allTables.index(activeTable)within the if statement gives the tables, I'm looking for.

The message is "Process finished with exit code 0"

like image 899
Bartli Avatar asked Dec 08 '17 14:12

Bartli


2 Answers

The solution is:

allTables = document.tables

for activeTable in allTables:
    if activeTable.cell(0,0).paragraphs[0].text == 'some text':
        activeTable._element.getparent().remove(activeTable._element)

Thanks to scanny.

like image 124
Bartli Avatar answered Sep 19 '22 23:09

Bartli


It sounds like your test if activeTable...text == 'some text' is not succeeding for any of the tables. In that case, the .remove() call is never executed but the script still returns an exit code of 0 (success).

Start by validating your test, maybe something like:

for table in document.tables:
    print("'%s'" % table.cell(0, 0).paragraphs[0].text)

and make sure the paragraph text is what you think it is. This should print out something like:

'some text but also some other text'
...

Once you have that determined, you may want to test on something other than the entire string, perhaps using .startswith():

text = table.cell(0, 0).paragraphs[0].text
if text.startswith('some text'):
    print('found one')

Once you have that working you can move on to the next problem.

like image 29
scanny Avatar answered Sep 18 '22 23:09

scanny