第18天---python办公自动化---读写word文件

第18天—python办公自动化—读写word文件

python-docx是一个在Python环境下读写word文档的“利器”。在使用前可以通过 pip install python-docx 命令安装,再通过import docx 引用即可。下面简单介绍一下 python-docx 的基本操作。

安装三方库python-docx —> pip install python-docx pillow
pillow —> PIL —> Python Image Library

想了解更多python-docx的知识,请看python-docx官方文档

使用python-docx创建和书写word文档

from docx import Document
from docx.shared import Cm, Pt

from docx.document import Document as Doc

document = Document()  # type: Doc
# 添加大标题
document.add_heading('快快乐乐学Python', 0)
# 添加段落
p = document.add_paragraph('Python是一门非常流行的编程语言,它')
run = p.add_run('简单')
run.bold = True
run.font.size = Pt(18)
p.add_run('而且')
run = p.add_run('优雅')
run.font.size = Pt(18)
run.underline = True
p.add_run('。')

# 添加一级标题
document.add_heading('Heading, level 1', level=1)
# 添加带样式的段落
document.add_paragraph('Intense quote', style='Intense Quote')
# 添加无序列表
document.add_paragraph(
    'first item in unordered list', style='List Bullet'
)
document.add_paragraph(
    'second item in ordered list', style='List Bullet'
)
# 添加有序列表
document.add_paragraph(
    'first item in ordered list', style='List Number'
)
document.add_paragraph(
    'second item in ordered list', style='List Number'
)

# 添加图片
document.add_picture('resources/R-C.jpg', width=Cm(5.2))

# 添加分节符
document.add_section()

records = (
    ('张三', '男', '1995-5-5'),
    ('李四', '女', '1992-2-2')
)
# 添加表格
table = document.add_table(rows=1, cols=3)
table.style = 'Dark List'
hdr_cells = table.rows[0].cells
hdr_cells[0].text = '姓名'
hdr_cells[1].text = '性别'
hdr_cells[2].text = '出生日期'
for name, sex, birthday in records:
    row_cells = table.add_row().cells
    row_cells[0].text = name
    row_cells[1].text = sex
    row_cells[2].text = birthday

# 添加分页符
document.add_page_break()
# 保存文档
document.save('resources/demo.docx')

document.add_section() —> 添加分节符

document.add_page_break() ----> 添加分页符

使用python-docx批量生成样板文章

先将模板换成上图类似的,便于我们使用python替换其中的内容

from docx import Document
from docx.document import Document as Doc

employees = [
    {
        'name': '张三',
        'id': '6666',
        'sdate': '2000年8月1日',
        'edate': '2021年5月30日',
        'department': '产品研发',
        'position': 'Python开发工程师',
    }
]

for emp_dict in employees:
    doc = Document('resources/离职证明模板.docx')  # type: Doc
    for p in doc.paragraphs:
        if '{' not in p.text:
            continue
        for run in p.runs:
            if '{' not in run.text:
                continue
            # 将占位符换成实际内容
            start, end = run.text.find('{'), run.text.find('}')
            key, place_holder = run.text[start + 1:end], run.text[start:end + 1]
            run.text = run.text.replace(place_holder, emp_dict[key])

    doc.save(f'resources/{emp_dict["name"]}离职证明.docx')

这样我们通过修改employees里面的数据就能批量生成文件,可以大大提高我们的工作效率,把繁琐而又重复的过程简单化。

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值