python实现多个excel合成合并为一个excel

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012209894/article/details/80097271

一、环境准备

Python版本:3.4

编辑器:Pycharm

excel:2007

二、python代码

 由于工作需要,将多个模板的excel整合成一个excel用于自动化数据导入,由于人工整合excel太麻烦,而且容易出错,所以决定通过python来处理这些繁杂的事务,5096.xls这个是新的模型,模型要先放进对应的文件夹才能生成,代码如下:

#!/usr/bin/env python
# coding=utf-8
import xlrd
import shutil
from xlutils.copy import copy
import datetime


class excel_cp:
    def __init__(self, ):
        day = datetime.date.today()
        self.str_day = str(day).replace('-', '')

    # 处理excel合并
    def excel_merge(self, old_name, new_name):
        if old_name == '降价通知.xls':
            lead_source = 'r510'
        elif old_name == 'BI5096.xls':
            lead_source = 'r953'
        elif old_name == '400溢出.xls':
            lead_source = 'r880'
        elif old_name == '潜客推荐.xls':
            lead_source = 'r722'
        else:
            lead_source = 'r520'
        old_dir = 'E:\\load_bi\\' + self.str_day + '\\' + old_name
        new_dir = 'E:\\load_bi\\' + self.str_day + '\\' + new_name
        # 打开要使用的excel,获取要需要写入的行数
        bk = xlrd.open_workbook(old_dir)
        sh = bk.sheet_by_name("Page 1")
        nrows = sh.nrows
        # 打开要插入的excel,获取sheet页面的行数,再获取输入的sheet
        oldWb = xlrd.open_workbook(new_dir, formatting_info=True)
        in_sheet = oldWb.sheet_by_name("Sheet1")
        in_nrows = in_sheet.nrows
        newWb = copy(oldWb)
        sheet = newWb.get_sheet(0)
        for i in range(1, nrows):
            row_data = sh.row_values(i)
            print(row_data)
            for j in (in_nrows-1+i, i+in_nrows-1):
            # ---------写出文件到excel--------
                print("-----正在往j写入 " + str(j) + " 行")
                sheet.write(j, 0, label=sh.cell_value(i, 4))   # 将old_dir的第i行第5列数据写入到new_dir第j行第1列
                sheet.write(j, 1, label=sh.cell_value(i, 1))   # 将old_dir的第i行第2列数据写入到new_dir第j行第2列
                sheet.write(j, 3, lead_source)                 # 将指定数据写入到new_dir第2行第4列
                sheet.write(j, 14, label=sh.cell_value(i, 2))  # 将old_dir的第i行第3列数据写入到new_dir第j行第15列
                sheet.write(j, 15, label=sh.cell_value(i, 3))  # 将old_dir的第i行第4列数据写入到new_dir第j行第16列
                sheet.write(j, 28, label=sh.cell_value(i, 7))  # 将old_dir的第i行第8列数据写入到new_dir第j行第29列
                sheet.write(j, 29, label=sh.cell_value(i, 8))  # 将old_dir的第i行第9列数据写入到new_dir第j行第30列
                sheet.write(j, 30, label=sh.cell_value(i, 9))  # 将old_dir的第i行第10列数据写入到new_dir第j行第31列
        newWb.save(new_dir)

    # 复制文件到指定路径
    def file_copy(self, rm_file):
        rm_file_dir = 'E:\\load_bi\\' + self.str_day + '\\' + rm_file
        new_file_dir = 'C:\\fakepath\\' + rm_file
        shutil.copyfile(rm_file_dir, new_file_dir)


if __name__ == '__main__':
    # @version : 3.4
    # @Author  : robot_lei
    # @Software: PyCharm Community Edition
    # 将这多个excel合并成一张
    old_file_name_s = ('降价通知.xls', 'BI5096.xls', '车库线索.xls', '400溢出.xls', '潜客推荐.xls')
    leads_source_str = ('r510', 'r953', 'r520', 'r880', 'r722')
    new_file_name = '5096.xls'
    for old_file_name in old_file_name_s:
        excel_cp().excel_merge(old_file_name, new_file_name)
    # 复制文件到指定路径
    excel_cp().file_copy(new_file_name)

三、合并结果

我这边是合并三个文件


猜你喜欢

转载自blog.csdn.net/u012209894/article/details/80097271