Search code examples
python-docx

python docx delete table from document


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"


Solution

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