python生成word文档的表格_python批量生成word文档

本文介绍了如何使用python-docx库创建和编辑docx文档,包括添加段落、标题、新页面和表格。还提到了python-docx-template库,用于动态生成和修改word文档,如动态表格、背景颜色、页眉页脚和图片替换等功能。
摘要由CSDN通过智能技术生成

一.python-docx

这是一个很强大的包,可以用来创建docx文档,包含段落、分页符、表格、图片、标题、样式等几乎所有的word文档中能常用的功能都包含了,这个包的主要功能便是用来创建文档,相对来说用来修改功能不是很强大,官网文档

安装

pip install python-docx

新建文档

from docx import Document

document = Document()

添加段落

from docx import Document

document = Document()

paragraph = document.add_paragraph('Lorem ipsum dolor sit amet.')

document.save('00.docx')

70

from docx import Document

document = Document()

paragraph = document.add_paragraph('Lorem ipsum dolor sit amet.')

document.add_paragraph('Lorem ipsum dolor sit amet.', style='ListBullet')

prior_paragraph = paragraph.insert_paragraph_before('Lorem ipsum')

document.save('00.docx')

70

添加标题

from docx import Document

document = Document()

document.add_heading('The REAL meaning of the universe',0)

document.add_heading('The role of dolphins', level=0)

document.add_heading('The role of dolphins', level=1)

document.add_heading('The role of dolphins', level=2)

document.add_heading('The role of dolphins', level=3)

document.add_heading('The role of dolphins', level=4)

document.save('00.docx')

70

新起一页

document.add_page_break()

添加表格

from docx import Document

document = Document()

##创建带边框的表格

table = document.add_table(rows=2, cols=2,style='Table Grid')

cell = table.cell(0, 1)

#第一行第二列

cell.text = 'parrot, possibly dead'

document.save('00.docx')

70

from docx import Document

document = Document()

##创建带边框的表格

table = document.add_table(rows=2, cols=2,style='Table Grid')

#第二行

row = table.rows[1]

row.cells[0].text = 'Foo bar to you.'

row.cells[1].text = 'And a hearty foo bar to you too sir!'

document.save('00.docx')

70

from docx import Document

document = Document()

##创建带边框的表格

table = document.add_table(1, 3,style='Table Grid')

#设置表格样式

table.style = 'LightShading-Accent1'

# populate header row

heading_cells = table.rows[0].cells

heading_cells[0].text = 'Qty'

heading_cells[1].text = 'SKU'

heading_cells[2].text = &#

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值