您的位置:首页 > 编程语言 > Python开发

18_python_练习题——写入文件到word文档中

2017-04-14 14:56 351 查看
想要使用python 将数据存储在doc 、docx等 word文档中,首先需要安装一个模块

python-docx

安装指令如下:

pip install python-docx
测试使用如下:

>>> from docx import Document
>>> document=docx.document
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'docx' is not defined
>>> document=Document()
>>> document.add_heading('Head',0)
<docx.text.paragraph.Paragraph object at 0x00000000034A4A20>
>>> document.add_heading('Head,help',1)
<docx.text.paragraph.Paragraph object at 0x00000000034A49E8>
>>> document.add_paragraph('Helllo world!')
<docx.text.paragraph.Paragraph object at 0x00000000034A4A58>
>>> document.save('hello_world.doc')

然后就可以生成doc文档了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: