Python合并多个Excel文件中的指定sheet

本文将介绍一个用于合并多个Excel文件中指定sheet的Python代码。这个功能可以方便地整理和分析数据。我们将逐步解释代码的每个部分,并提供示例用法。

导入库

首先,我们导入了需要使用的三个库:ospandastime。这些库分别用于操作文件和文件夹、处理Excel文件以及计算程序执行时间。

import os
import pandas as pd
import time

定义函数

我们定义了一个名为merge_excel_sheets的函数,用于将多个Excel文件中的指定sheet合并到一个新的Excel文件中。该函数接受三个参数:folder_path(文件夹路径)、excel_list(包含要合并的Excel文件和sheet名的列表)和output_file(输出文件路径)。

def merge_excel_sheets(folder_path, excel_list, output_file):
    start_time = time.time()

    with pd.ExcelWriter(output_file) as writer:
        for excel_name, sheet_name in excel_list: file_path = os.path.join(folder_path, excel_name) df = pd.read_excel(file_path, sheet_name=sheet_name) sheet_name_combined = f"{excel_name[:-5]}-{sheet_name}" df.to_excel(writer, sheet_name=sheet_name_combined, index=False) end_time = time.time() execution_time = end_time - start_time print(f"程序执行时间:{execution_time}秒")

在函数内部,我们首先记录程序开始执行的时间。然后,我们使用pd.ExcelWriter创建一个空的Excel Writer对象,用于写入合并后的数据。

start_time = time.time()

with pd.ExcelWriter(output_file) as writer:

接下来,我们使用一个循环来处理每个Excel文件和sheet。对于每个文件和sheet,我们构造完整的文件路径,并使用pd.read_excel读取数据并存储为DataFrame对象。

for excel_name, sheet_name in excel_list:
    file_path = os.path.join(folder_path, excel_name)
    df = pd.read_excel(file_path, sheet_name=sheet_name)

然后,我们构造合并后的sheet名称,格式为"原文件名-原sheet名",并使用df.to_excel将DataFrame对象中的数据写入到指定的sheet中。

sheet_name_combined = f"{excel_name[:-5]}-{sheet_name}"
df.to_excel(writer, sheet_name=sheet_name_combined, index=False)

最后,我们计算程序执行的时间,并将其打印出来。

end_time = time.time()
execution_time = end_time - start_time
print(f"程序执行时间:{execution_time}秒")

示例用法

我们提供了一个示例用法,包括文件夹路径、要合并的Excel文件和sheet的列表,以及输出文件路径。通过调用merge_excel_sheets函数,我们可以执行合并操作。

folder_path = "E:\\工作内容"
excel_list = [ ("一店9月.xlsx", "原始数据"), ("二店9月.xlsx", "原始"), ("三店9月.xlsx", "原始数据"), ("四店9月.xlsx", "原始数据"), ("五店9月-离职.xlsx", "原始数据") ] output_file = os.path.join(folder_path, "output.xlsx") merge_excel_sheets(folder_path, excel_list, output_file)

完整代码

import os
import pandas as pd  # 导入pandas库
import time  # 导入时间库,用于计算程序执行时间

def merge_excel_sheets(folder_path, excel_list, output_file):
    start_time = time.time()  # 记录程序开始执行的时间

    # 创建一个空的Excel Writer对象,用于写入合并后的数据
    with pd.ExcelWriter(output_file) as writer: # 循环处理每个Excel文件和sheet for excel_name, sheet_name in excel_list: # 根据文件名和文件夹路径,构造完整的文件路径 file_path = os.path.join(folder_path, excel_name) # 读取指定Excel文件中指定sheet的数据,并存储为DataFrame类型的对象 df = pd.read_excel(file_path, sheet_name=sheet_name) # 构造合并后的sheet名称,格式为"原文件名-原sheet名" sheet_name_combined = f"{excel_name[:-5]}-{sheet_name}" # 将DataFrame对象中的数据写入到指定sheet中 df.to_excel(writer, sheet_name=sheet_name_combined, index=False) end_time = time.time() # 记录程序结束执行的时间 execution_time = end_time - start_time # 计算程序执行的时间 print(f"程序执行时间:{execution_time}秒") # 输出程序执行的时间 # 示例用法 folder_path = "E:\\工作内容" excel_list = [ ("一店9月.xlsx", "原始数据"), ("二店9月.xlsx", "原始"), ("三店9月.xlsx", "原始数据"), ("四店9月.xlsx", "原始数据"), ("五店9月-离职.xlsx", "原始数据") ] output_file = os.path.join(folder_path, "output.xlsx") merge_excel_sheets(folder_path, excel_list, output_file) # 调用合并函数,将指定的Excel文件中指定sheet的数据进行合并
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值