python操作word我们需要用到python-docx这个库。

安装命令:(打开控制台运行)

pip install python-docx

官方demo:

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('monty-truth.png', width=Inches(1.25))

# 添加表格

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 item in recordset:

row_cells = table.add_row().cells

row_cells[0].text = str(item.qty)

row_cells[1].text = str(item.id)

row_cells[2].text = item.desc

#添加分页符

document.add_page_break()

#保存文档

document.save('demo.docx')

效果如下:

看了上面的官方demo就能直接上手使用了。

不过我建议自己可以按需封装一下方法

from docx import Document

from docx.shared import Inches

from docx.enum.text import WD_ALIGN_PARAGRAPH

from docx.shared import Pt

from docx.oxml.ns import qn

document = Document()

#设置文档默认字体

document.styles['Normal'].font.name = u'微软雅黑'

document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'微软雅黑')

title = document.add_paragraph()

#大标题居中

title.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER

# 参数

#文档对象,文字内容,文字大小,文字样式(目前就只判断了粗体)

defwriteP(document, content, size, style = None):

p = document.add_paragraph()

run = p.add_run(content)

font = run.font

font.size = Pt(size)

if style == 'bold':

font.bold = True

好了,python操作word就介绍到这里,大家可以去尝试一下。