美文网首页
Python 处理 Word文档

Python 处理 Word文档

作者: 白鬓少年 | 来源:发表于2020-05-07 16:56 被阅读0次

方法一: python-docx

参考:python-docx 0.8.10 documentation

pip install python-docx

官方实例:

代码:
from docx import Document
from docx.shared import Inches

document = Document()  # 创建模板,也可使用已存在的template

document.add_heading('Document Title', 0)  # 添加0级标题

p = document.add_paragraph('A plain paragraph having some ')  # 添加段落
p.add_run('bold').bold = True  # p段落后添加文本”bold“并设置为粗体
p.add_run(' and some ')  # 继续添加文本” and some"
p.add_run('italic.').italic = True  # 添加斜体文本“italic."并以设置为斜体

document.add_heading('Heading, level 1', level=1)  # 添加1级标题
document.add_paragraph('Intense quote', style='Intense Quote')  # 添加Intens quote

document.add_paragraph(
    'first item in unordered list', style='List Bullet'
)  
document.add_paragraph(
    'first item in ordered list', style='List Number'
)

document.add_picture('monty-truth.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')
官方实例效果.png

实践记录:

import datetime
from docx import Document


now = datetime.datetime.now()  # 获取当前时间
document = Document('yj_template.docx')  # 获取模板yj_template.docx
rows = [row.text for row in document.paragraphs]  # 获取模板中文本,按行存入列表rows

# 在第一行中填写今天的日期
rows[0] = rows[0].replace(' 月', f'{now.month}月')
rows[0] = rows[0].replace(' 日', f'{now.day}日')

# 写入当天14:00数据
stcd_map = {'90527600': 2,
        '90527650': 3,
        '90520600': 4,
        '90527800': 5}

data = get_data(today_str + ' 14:00')
data.replace(stcd_map, inplace=True)
data.set_index('STCD', inplace=True)

#  填写水位(保留两位小数)、流量数据
for i in data.index:
    rows[i] = rows[i].replace('     m,', f'{data.loc[i].Z: .2f}m,')
    rows[i] = rows[i].replace('      m', f' {data.loc[i].Q: g}m')

#  修改document.parapraphs
for i in range(len(rows)):
    document.paragraphs[i].text = rows[i]

# 上标无法正常显示,进一步处理:
row_num = [2, 3, 4, 5]
for i in row_num:
    document.paragraphs[i].text = rows[i].replace('m3/s', 'm')  # 去掉行尾的'3/s'
    # *添加上标'3'
    super_text = document.paragraphs[i].add_run('3') 
    super_text.font.superscript = True

    document.paragraphs[i].add_run(r'/s')

#  按当前日期保存docx文件
file_date = now.strftime('%Y-%m-%d') + r' 14'
document.save(f'yj水情({file_date}).doc')

方法二:docx-mailmerge

参考:# Populating MS Word Templates with Python

conda install lxml
pip install docx-mailmerge
示例
创建模板:
创建模板.png
在模板中添加域I_am_here.png
域名效果.png
使用模板:
from __future__ import print_function
from mailmerge import MailMerge
from datetime import date

template = "Practical-Business-Python.docx"  # 已创建的模板路径
document = MailMerge(template)   # 获取模板

print(document.get_merge_fields())  # 打印文档中所有域的名字 {'I_am_here'}

# 填写对应域的值,可用字典形式
document.merge(I_am_here = '白鬓少年')

document.write('test-output.docx')  #  保存doc文档
test-output.png

相关文章

网友评论

      本文标题:Python 处理 Word文档

      本文链接:https://www.haomeiwen.com/subject/ufsaghtx.html