官方小demo
https://python-docx.readthedocs.io/en/latest/

#!/usr/bin/env python
# -*- coding:utf8 -*-
# @TIME     :2019/5/9 10:42
# @Author   :17976
# @File     :simpleWriteDoc.py 
# @Description:
from docx import Document
from docx.shared import Inches
document = Document()
# 文档标题
document.add_heading('Document Title', 0)
# 添加段落
p = document.add_paragraph('A plain paragraph having some ')
# 加粗
p.add_run('bold').bold = True
p.add_run(' and some ')
# 斜体
p.add_run('italic.').italic = True
# 一级标题
document.add_heading('Heading, level 1', level=1)
# 加粗和下划线
document.add_paragraph('Intense quote', style='IntenseQuote')
document.add_paragraph(
    'first item in unordered list', style='ListBullet'
)
document.add_paragraph(
    'first item in ordered list', style='ListNumber'
)
# 添加图片
document.add_picture('img.png', width=Inches(1.25))
#添加表格
records = (
    (3, '101', 'Spam'),
    (7, '422', 'Eggs'),
    (4, '631', 'Spam, spam, eggs, and spam')
)
table = document.add_table(rows=1, cols=3)
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
document.add_page_break()
document.save('demo.docx')

https://www.cnblogs.com/hupeng1234/p/6920129.html
用python-docx模块读写word文档
https://www.jianshu.com/p/94ac13f6633e
高级用法:
https://blog.csdn.net/qq_19648627/article/details/73811204

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐