from win32com.client import Dispatch, constants import win32clipboard as wc doc_app = Dispatch("kwps.Application") doc_app.Visible = False doc = doc_app.Documents.Open(r'C:\Users\meiya\PycharmProjects\copyExcelToWord_V2\项目产品规格说明书模板(1).docx') #添加标题4.2 location = doc.Range(3188, 3188) parag = doc.Paragraphs.Add(Range = location)#添加新得段落 parag_range = parag.Range parag_range.Text ='标题1' #设置样式 parag_range.Style = doc.Styles("R&D二级标题")#英文版word的标题 #添加标题4.2.1 parag_range.Text +='\n' location = parag_range parag = doc.Paragraphs.Add(Range = location) parag_range = parag.Range parag_range.Text ='标题2' parag_range.Style = doc.Styles("R&D三级标题")#二级标题 #添加标题4.2.1.1 titlelist = ['功能概述','用户与权限','执行条件','界面要素和处理内容','状态图','附加说明'] for i in titlelist: parag_range.Text +='\n' location = parag_range parag = doc.Paragraphs.Add(Range=location) parag_range = parag.Range parag_range.Text = i parag_range.Style = doc.Styles("R&D四级标题") doc.Save() doc.Close() doc_app.Quit()
标签:word,python,doc,Range,标题,range,win32com,location,parag From: https://www.cnblogs.com/zhangmeiyan/p/16620038.html