python的docx用于处理word文件比较方便,基本的内容读写可以更快捷地促进文档化工作。
install
- 安装依赖包
pip install python-docx
read
doc = docx.Document("xxx.docx")
# 读取内容
paras = doc.paragraphs
# 读取表格
tables = doc.tables
write
doc = Document()
doc.add_heading("Head 标题", 0)
doc.add_heading("次级标题", 1)
doc.add_paragraph('Intense quote', style='Intense Quote')
# normal
doc.add_paragraph("paragraph-one")
# unordered
p = doc.add_paragraph("paragraph-two")
p.style='ListBullet'
p.add_run('content').bold = True
# ordered
p2 = doc.add_paragraph("paragraph-three", style='ListNumber')
records = (
(3, '101', 'Spam'),
(7, '422', 'Eggs'),
(4, '631', 'Spam, spam, eggs, and spam')
)
table = doc.add_table(rows=1, cols=3)
table.style = 'LightShading-Accent1'
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
for qty, id, desc in records:
row_cells = table.add_row().cells
row_cells[0].text = str(qty)
row_cells[1].text = id
row_cells[2].text = desc
doc.add_picture("xxx.png", width=Inches(6.0))
doc.add_page_break()
doc.save("xxx.docx")