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()
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/282017.html