点击上方“AI搞事情”关注我们
思路
主要先运用Find.Execute
定位需要替换的文档文字内容,再对选中的文字区域app.Selection
设置字体格式Font.Color = 255
![](https://i-blog.csdnimg.cn/blog_migrate/6fc1eae697823c66a80056db43f5a0cd.png)
代码
import os
import win32com.client
docx_path = 'test.docx'
app = win32com.client.DispatchEx("Kwps.Application")
# app.Visible = True
# app.ScreenUpdating = True
doc = app.Documents.Open(os.path.abspath(docx_path))
words = [u'绿色', u'蓝色'] # 需要替换颜色的文字内容
for word in set(words):
while app.Selection.Find.Execute(word, False, False, False, False, False, True, 0, True, "", 0):
app.Selection.Font.Color = 255 # 替换为红色
doc.Range(0, 0).Select()
doc.SaveAs2()
doc.Close()
app.Quit()
![](https://i-blog.csdnimg.cn/blog_migrate/a57cf46ada5414f4586e801f7b89202b.png)
参考
❝
用python脚本如何改变word中特定词的字体颜色?
https://zhidao.baidu.com/question/622421663610499092.html
长按二维码关注我们
有趣的灵魂在等你