读取多个excel文件追加至新的excel中

办公自动化02

​ 工作中会遇到一些需求,面临这些需求可以使用python进行自动化处理,比如下面的一些需求。

2.读取多个excel文件追加至新的excel中
"""
-*- coding: utf-8 -*-
-- @author: phil
-- @Time:2022/5/14 14:16
-- 需求:读取多个excel文件,追加写入新的sheet中,实现文件合并
-- 1.读取多个excel文件
-- 2.追加写入新的sheet中
-- ## v_2
"""
import os
import time
import openpyxl

class Read_excel_Write():

    def Get_file_path(self, filePath):
        '''
        #     :param filePath: 工作薄的路径
        #     :return file_path: 文件路径列表
        #     '''
        try:
            name = os.listdir(filePath)
            print(len(name))
            file_path = []
            for i in range(len(name)):
                file_path.append(filePath + '\\' + name[i])
            print('>>>>>>> 文件路径已经获取!')
            return file_path
        except Exception as e:
            print(e)

    def Read_and_Write(self, path_list):
        '''
       #     :param path_list: 工作薄的路径
       #     :return:
       #     '''
        # 获取第一个表的行列值
        wb_first = openpyxl.load_workbook(path_list[0])
        sheets_first = wb_first.sheetnames                            # 取得工作簿中所有表名的列表
        print('取得工作簿中所有表名的列表:' + '{}'.format(sheets_first))   # ['sheet1']
        sheetName = sheets_first[0]                                   # 获取指定表名称
        first_object = wb_first[sheetName]                            # 获取指定表对象
        # 获取行列数量
        first_nrows = first_object.max_row
        first_ncolumns = first_object.max_column
        print('当前基表的最大行数:' + '{}'.format(first_nrows))     # 获得最大行数
        print('当前基表的最大列数:' + '{}'.format(first_ncolumns))  # 获得最大行数

        # 开始遍历文件路径列表
        for i in range(1, len(path_list)):
            print('当前任务的文件路径为:{}'.format(path_list[i]))
            # 第一阶段:对Excel内容进行读取
            wb = openpyxl.load_workbook(path_list[i])
            sheets = wb_first.sheetnames                       # 取得工作簿中所有表名的列表
            sheet_Name = sheets[0]                             # 获取指定表名称
            sheet_object = wb[sheet_Name]                      # 获取指定表对象
            # 获取遍历表的最大行数
            circle_nrows = sheet_object.max_row
            # circle_rows = sheet_object.rows  # 获得行数 类型为迭代器
            print('当前遍历表的最大行数:' + '{}'.format(circle_nrows-1))   # 获得最大行数

            # 第二阶段:读写结合

            for j in range(1, circle_nrows): 
                first_nrows = first_nrows + 1
                for n in range(first_ncolumns):
                    first_object.cell(first_nrows, n+1).value = sheet_object.cell(j+1, n+1).value
                    print(sheet_object.cell(j+1, n+1).value, "\t", end="")
                print()

            # 更新基表的最新内容最大行数
            print('当前基表的最大行数:' + '{}'.format(first_nrows))  # 获得最大行数

            # 结果输出:设置一个写入任务完成时间显示
            gm_time2 = time.gmtime()
            now_time2 = time.strftime("%Y-%m-%d %H:%M:%S", gm_time2)
            print('\n' + '>>>>>>>{} 第{}个文件表的写入任务完成!'.format(now_time2, i))

        wb_first.save(path_list[0])

if __name__ == '__main__':
    filePath = 'C:\\Users\\******\\Desktop\\excel测试'
    EW = Read_excel_Write()
    path_list = EW.Get_file_path(filePath)
    EW.Read_and_Write(path_list)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值