I have a huge Word Document which basically consists of chapters and in each chapter I only have a huge table with data.
Secondly, I have a python list like data = ['word1','word2','word3']
.
My goal is now to loop through every cell of every table in the word document and if I want any occurence of words in the data
list, I want to make this word , keeping all existing text of the same cell as is.
Example:
Header1 | Header2 |
---|---|
Some text word1 some more text | Blabla |
Foo word2 word3 some bar | blabla |
Expected result:
Header1 | Header2 |
---|---|
Some text some more text | Blabla |
Foo some bar | blabla |
I tried using the following code (as a starting point to find only 1 word instead of full list):
from docx import Document
f = open(myfile,'rb')
doc = Document(f)
for t in doc.tables:
for col in t.columns:
for cell in col.cells:
if 'word1' in cell.text:
print(cell.text)
txt = cell.text
new = cell.add_paragraph()
runner = new.add_run(txt)
runner.bold=True
doc.save(outfile)
f.close()
But it does not replace existing text, it just adds new, bold text afterwards and - even worse - it makes the complete cell text bold not only the key word.
