首先安装docx包,命令:pip install python-docx

通过创建对象进行数据的插入,包括文字、图片、表格、段落等内容。
import docx
doc2 = docx.Document() # 创建一个Document对象
doc2.add_paragraph('time') # 增加一个paragraph
# 插入有序列表,段落的前面会有序号123
doc2.add_paragraph('把冰箱门打开', style='List Number')
doc2.add_paragraph('把大象装进去', style='List Number')
doc2.add_paragraph('把冰箱门关上', style='List Number')
# 插入无序列表,段落的前面没有序号
doc2.add_paragraph('把冰箱门打开', style='List Bullet')
doc2.add_paragraph('把大象装进去', style='List Bullet')
doc2.add_paragraph('把冰箱门关上', style='List Bullet')
# 插入一个6行6列的表格
table = doc2.add_table(rows=6, cols=6, style='Table Grid')
for i in range(0, 6):
for j in range(0, 6):
table.cell(i, j).text = "第{i}行{j}列".format(i=i + 1, j=j + 1)
# 插入照片
doc2.add_picture('1.JPG', width=docx.shared.Inches(5))
doc2.save('111.docx') # 保存文档
实现的效果图:

网友评论