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

本文介绍如何使用Python的python-docx模块创建Word文档。通过安装该模块并利用其提供的Document类,可以轻松地向文档中添加标题和段落,并保存为doc格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

想要使用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文档了
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值