怎么用python3编辑word文档?
1、python想要编辑word文档,需要安装docx模块。
下图的情景,表示我的电脑上已经安装了这个模块了。

2、用python新建一个空白的word文档:
#coding=utf-8
from docx import Document
test = Document()
test.save(u'C:/Users/Administrator/Desktop/第一篇.docx')

3、往空白的word文档里面,加入“第一段文字!”:
p = test.add_paragraph(u'第一段文字!')

4、打开word文档,可以看到里面已经有一段文字了。

5、设置字体,需要载入Pt模块:
from docx.shared import Pt
然后,在word文档里面换行输入“24号字体”:
run = p.add_run(u'\n24号字体!')
run.font.size = Pt(24)

6、word文档里面的情形:

7、要设置中文字体,需要载入qn模块:
from docx.oxml.ns import qn
然后在word文档里面换行输入72号的楷体的“中文字体”:
run = p.add_run(u'\n中文字体')
run.font.name=u'楷体'
run.font.size = Pt(72)
r = run._element
r.rPr.rFonts.set(qn('w:eastAsia'), u'楷体')

8、word文档里面:

9、用python读取word文档里面的内容:
from docx import Document
dir_docx = 'C:/Users/Administrator/Desktop/第一篇.docx'
dd = Document(dir_docx)
for p in dd.paragraphs:
print (p.text)
读的时候,只提取文本,而忽略各种格式。
