'How to replace a word in a Word document and change its font and color using python?

I want to replace a word in a .docx file and change the format of the same word.

Currently, the word is highlighted in the document, I am able to replace the word but the formatting and highlight color remains the same.

Here's the code I'm using.

import docx
from docx.shared import Pt
from docx import Document

#open the document
doc=Document('C:\RPA\Commercial\File.docx')
Dictionary = {"CLD":"amit"}
for i in Dictionary:
    for p in doc.paragraphs:
        if p.text.find(i)>=0:
            p.text=p.text.replace(i,Dictionary[i])
            styles = doc.styles
            style = doc.styles['Normal']
            font = style.font
            font.name = 'Times New Roman'
            font.size = Pt(20)
#save changed document
doc.save('C:\RPA\Commercial\File.docx')


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source