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的数据进行合并
相关推荐
程序员大雄学编程20 分钟前
「用Python来学微积分」5. 曲线的极坐标方程
开发语言·python·微积分
一位代码1 小时前
python | requests爬虫如何正确获取网页编码?
开发语言·爬虫·python
可触的未来,发芽的智生1 小时前
新奇特:神经网络速比器,小镇债务清零的算法奇缘
javascript·人工智能·python
mortimer2 小时前
还在被 Windows 路径的大小写和正反斜杠坑?是时候让 pathlib 拯救你的代码了!
人工智能·python
std860212 小时前
Rust 与 Python – 这是未来的语言吗?
开发语言·python·rust
鄃鳕2 小时前
Flask【python】
后端·python·flask
weixin_46682 小时前
Python编程之面向对象
开发语言·人工智能·python
Lynnxiaowen3 小时前
今天我们学习python编程常用模块与面向对象
运维·python·学习·云计算
一头生产的驴3 小时前
java整合itext pdf实现固定模版pdf导出
java·python·pdf
魔都吴所谓3 小时前
【python】快速实现pdf批量去除指定位置水印
java·python·pdf