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"
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.
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.
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