python调用word接口主要用到的模板为python-docx,基本操作官方文档有说明。
使用python新建一个word文档,操作就像文档里介绍的那样:
1 from docx importDocument2 from docx.shared importInches3
4 document =Document()5
6 document.add_heading('Document Title', 0) #插入标题
7
8 p = document.add_paragraph('A plain paragraph having some') #插入段落
9 p.add_run('bold').bold =True10 p.add_run('and some')11 p.add_run('italic.').italic =True12
13 document.add_heading('Heading, level 1', level=1)14 document.add_paragraph('Intense quote', style='IntenseQuote')15
16 document.add_paragraph(17 'first item in unordered list', style='ListBullet'
18 )19 document.add_paragraph(20 'first item in ordered list', style='ListNumber'
21 )22
23 document.add_picture('monty-truth.png', width=Inches(1.25)) #插入图片