python办公自动化(win32com):word中查找关键字所在段落,并设置段落内容的样式
作者:互联网
from win32com import client # *** wps用kwps.Application;Microsoft word用Word.Application *** doc_app = client.Dispatch("kwps.Application") doc_app.Visible = False doc = doc_app.Documents.Open(r'C:\Users\meiya\PycharmProjects\copyExcelToWord_V2\CFIT-SDP0302_XX项目产品规格说明书模板(1).docx') fullrange = doc.Range() fullrange.Find.Execute('功能清单') fullrange.Find.Execute('功能清单') p= fullrange.Paragraphs(1) print(p) pnext = p.Next() print(pnext) # 遍历可以查找出所有包含关键字的段落 # 去掉遍历只查找到第一个包含关键字的段落 fullrange = doc.Range() for i in range(4): fullrange.Find.Execute('测试') p= fullrange.Paragraphs(1) print(p) # 找到目标段落后,设置段落内容的样式 while fullrange.Find.Execute('测试'): p = fullrange.Paragraphs(1) p.Range.Font.Name = '楷体' p.Alignment = 1 p.CharacterUnitFirstLineIndent = -2 p.LineUnitAfter = 3 p.LineUnitBefore = 5 p.LineSpacingRule = 2 p.OutlineLevel =1 # 返回文档的所有样式并输出 styles = doc.Styles for i in styles: print(i) doc.Save() doc.Close() doc_app.Quit()
标签:段落,Execute,word,python,doc,app,print,Find,fullrange 来源: https://www.cnblogs.com/zhangmeiyan/p/16620062.html