前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python办公自动化 | 批量word报告生成工具

Python办公自动化 | 批量word报告生成工具

作者头像
披头
发布2020-01-14 16:08:03
8.3K0
发布2020-01-14 16:08:03
举报
文章被收录于专栏:datartisandatartisan

有时候我们需要按照某种规则生成一种固定模板的word报告,python能够很好的完成这项工作。本文通过一个小示例说明一下如何通过Python实现自动生成word报告。

首先我们需要有一个word报告模板,模板中内置了一些需要修改的关键字,类似这个样子

如上图所示,文档中标红的文字都属于关键字,是需要替换的。

这里,我们还需要一份excel表格,用来存储报告的关键内容。

到这里,准备工作就做好了,可以开始写代码了。

处理word需要用到python-docx包,先pip安装

代码语言:javascript
复制
pip install python-docx

首先导入用到的包

代码语言:javascript
复制
from docx import Document
import xlrd

编写一个小函数来实现word段落内容和表格内容的替换

代码语言:javascript
复制
def text_chenge(headline, data):
    # 用来替换word段落中的关键字内容,关键字都是excel表格的标题行
    myparagraphs = document.paragraphs
    for paragraph in myparagraphs:
        for run in paragraph.runs:
            run_text = run.text.replace(headline, data)
            run.text = run_text

    # 用来替换word表格中的关键字内容,关键字都是excel表格的标题行
    mytables = document.tables
    for table in mytables:
        for row in table.rows:
            for cell in row.cells:
                cell_text = cell.text.replace(headline, data)
                cell.text = cell_text

打开excel文件,获取报告内容

代码语言:javascript
复制
xlsx = xlrd.open_workbook(r'D:\pycharm\learning\autowork\document\报告数据.xlsx')
table = xlsx.sheet_by_index(0)

遍历excel的单元格,同时打开报告模板文件,按照excel中的数据替换报告模板中的关键字,替换完成后,保存为新文件,文件名为excel中的A列单元格的内容+eSRVCC切换成功率低优化报告.docx 实现代码如下:

代码语言:javascript
复制
xlsx = xlrd.open_workbook(r'D:\pycharm\learning\autowork\document\报告数据.xlsx')
table = xlsx.sheet_by_index(0)

for table_row in range(1, table.nrows):
    document = Document(r'D:\pycharm\learning\autowork\document\报告模板.docx')
    for table_col in range(0, table.ncols):
        text_chenge(str(table.cell(0, table_col).value), str(table.cell(table_row, table_col).value))
        # 将excel表格中的内容替换掉标题行,因为标题行即为报告模板中的关键字

    document.save(f'{str(table.cell(table_row, 0).value)} eSRVCC切换成功率低优化报告.docx')
    print("%s eSRVCC切换成功率低优化报告成功生成!" % str(table.cell_value(table_row, 0)))

执行代码,即可在相同目录下生成多个word报告

完整代码如下:

代码语言:javascript
复制
from docx import Document
import xlrd


def text_chenge(headline, data):
    # 用来替换word段落中的关键字内容,关键字都是excel表格的标题行
    myparagraphs = document.paragraphs
    for paragraph in myparagraphs:
        for run in paragraph.runs:
            run_text = run.text.replace(headline, data)
            run.text = run_text

    # 用来替换word表格中的关键字内容,关键字都是excel表格的标题行
    mytables = document.tables
    for table in mytables:
        for row in table.rows:
            for cell in row.cells:
                cell_text = cell.text.replace(headline, data)
                cell.text = cell_text


xlsx = xlrd.open_workbook(r'D:\pycharm\learning\autowork\document\报告数据.xlsx')
table = xlsx.sheet_by_index(0)

for table_row in range(1, table.nrows):
    document = Document(r'D:\pycharm\learning\autowork\document\报告模板.docx')
    for table_col in range(0, table.ncols):
        text_chenge(str(table.cell(0, table_col).value), str(table.cell(table_row, table_col).value))
        # 将excel表格中的内容替换掉标题行,因为标题行即为报告模板中的关键字

    document.save(f'{str(table.cell(table_row, 0).value)} eSRVCC切换成功率低优化报告.docx')
    print("%s eSRVCC切换成功率低优化报告成功生成!" % str(table.cell_value(table_row, 0)))
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-01-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 乐享数据8090 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档