I want to replace text in my word document. I am able to replace text strings which are matching completely, but I want to replace it if it will match 90% with the searched string.
I am using python-docx for working with Word documents.
Below code replaces text in my word document if it matches completely.
Code link
def docx_replace_regex(doc_obj, regex , replace):
for p in doc_obj.paragraphs:
if regex.search(p.text):
inline = p.runs
# Loop added to work with runs (strings with same style)
for i in range(len(inline)):
if regex.search(inline[i].text):
text = regex.sub(replace, inline[i].text)
#inline[i].text = text.decode('UTF-8')
inline[i].text = text
for table in doc_obj.tables:
for row in table.rows:
for cell in row.cells:
docx_replace_regex(cell, regex , replace)
I am not getting a proper way to replace/substitute the partially matched string.
Any kind of help is much appreciated.
Thanks in advance.