I am using python-docx to manipulate word documents. Here is what I currently have to modify text in normal paragraphs:
doc = Document('idk.docx')
for paragraph in doc.paragraphs:
if "oldtext1" in paragraph.text:
paragraph.replace("oldtext1","Something")
if "oldtext2" in paragraph.text:
paragraph.replace("oldtext2","Somethingelse")
If I want to modify the text in a table, I need to do the following
tables = doc.tables
for table in tables:
for row in table.rows:
for cell in row.cells:
for paragraph in cell.paragraphs:
if "oldtext1" in paragraph.text:
paragraph.replace("oldtext1","Something")
if "oldtext2" in paragraph.text:
paragraph.replace("oldtext2","Somethingelse")
The code works fine and the text is replaced but the problem is that I am trying to replace ALL instances of the text in the document and I do not want to have 2 separate loops (1 for normal text in paragraphs and another for text in tables)
Is there an easy way to combine these loops so I do not have to have the same if-statements in 2 different loops?