再也不用手动拷贝处理excel表格数据到word表格了

548 阅读2分钟

来掘金的第一篇文章,只为好玩。

最近帮老婆新工作需要频繁处理excel数据粘贴到word中,而且粘贴部分列字段,到word中的表格中,看着那手动复制粘贴,作为程序员真心难以接受,考虑到老婆深陷到这种工作,无法照顾家庭,我决定帮他搞定。

处理这些excel,word数据,当然首选python,什么java,js都是弟弟。

废话不说,来看一下要处理的数据

image.png

需要做的数据是将上面红色箭头三列,粘贴到如下文档红色框框中

image.png

我的整体思路是,读取到表格中的数据存到数组中,然后将数组数据通过插入到word中。

第1步 安装需要使用的库

xlrd,和python-docx

备注:xlrd 不支持xlxs,仅支持xls

第2步读取excel,数据,

import xlrd ## 处理excel用的库是xlrd
def get_excel_data(file):
    workbook = xlrd.open_workbook(file) #路径
    table = workbook.sheets()[0]# 读取第几个sheet 0开始
    rows = table.nrows # 读取行数
    data_arr = []
    # 注释
    for row in range(1, rows):
      #  table.cell_value(行,列) 都是0开始
        temp = {};
        temp['name'] = table.cell_value(row,0)
        temp['progress'] = table.cell_value(row, 2)
        temp['result'] = table.cell_value(row, 3)
        data_row = {'name': int(table.cell_value(row, 0)), 'progress': table.cell_value(row, 1),
                    'result': table.cell_value(row, 2)}
        data_arr.append(data_row)
    print(data_arr)
    return data_arr
 excel_data = get_excel_data('../../data/软件测评表格数据.xls');

第二部 数据插入到word

from docx import Document ## 处理word用到的库是python-docx
def write_data_2_doc(data_arr, file_name):
    document = Document(file_name)
    tables = document.tables
    for index in range(len(data_arr)):
        wordTableRows = len(tables[0].rows)
        if wordTableRows < (index + 2):
            tables[0].add_row()
        tables[0].cell(index + 1, 0).text = str(data_arr[index]['name'])
        tables[0].cell(index + 1, 1).text = data_arr[index]['progress']
        tables[0].cell(index + 1, 2).text = data_arr[index]['result']
    document.save(file_name)
write_data_2_doc(excel_data, '../../data/测评文档初版.docx')

下面给一下完整代码

import xlrd
# coding:utf-8
from docx import Document


def get_excel_data(file):
    workbook = xlrd.open_workbook(file) #路径
    table = workbook.sheets()[0]# 读取第几个sheet 0开始
    rows = table.nrows # 读取行数
    data_arr = []
    # 注释
    for row in range(1, rows):
      #  table.cell_value(行,列) 都是0开始
        temp = {};
        temp['name'] = table.cell_value(row,0)
        temp['progress'] = table.cell_value(row, 1)
        temp['result'] = table.cell_value(row, 2)
        data_row = {'name': int(table.cell_value(row, 0)), 'progress': table.cell_value(row, 1),
                    'result': table.cell_value(row, 2)}
        data_arr.append(data_row)
    print(data_arr)
    return data_arr


def write_data_2_doc(data_arr, file_name):
    document = Document(file_name)
    tables = document.tables
    for index in range(len(data_arr)):
        wordTableRows = len(tables[0].rows)
        if wordTableRows < (index + 2):
            tables[0].add_row()
        tables[0].cell(index + 1, 0).text = str(data_arr[index]['name'])
        tables[0].cell(index + 1, 1).text = data_arr[index]['progress']
        tables[0].cell(index + 1, 2).text = data_arr[index]['result']
    document.save(file_name)


if __name__ == "__main__":
    excel_data = get_excel_data('../../data/软件测评表格数据.xls');
    print("需要抽取的数据\n", excel_data)
    write_data_2_doc(excel_data, '../../data/测评文档初版.docx')
    print("抽取数据插入完成")

至此搞定,避免了频繁复制粘贴,解放了老婆,可以看娃了,我可以摆脱娃娃了。 最近看娃娃看的我肝疼。