1.Python写word文档
- 要操作word文档首先需要安装python-docx库;
pip install python-docx
- 然后导入docx模块,或者从docx模块中导入Document类;
from docx import Document
- 然后使用
Document()
创建一个word文档,若指定路径则是打开文档;
document = Document()
- 接着就可以在文档中插入内容,比如插入标题可以使用
add_heading()
方法,其中参数level是标题等级,0表示一级标题,1表示二级标题,以此类推。
插入段落可以使用add_paragraph()
方法,参数style是样式,默认不应用样式。
还有其他例如add_picture()
方法用来插入图片,add_table()方法插入表格等。
最后和操作Excel一样在文档中添加完内容之后需要使用save('文件名')
方法保存文档;
大家可以自己查看官网:https://python-docx.readthedocs.io/en/latest/
from docx import Document
document = Document()
#