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的数据进行合并
相关推荐
清水白石0084 小时前
从“类型体操”到工程设计:用 Python 解释协变、逆变与不变
网络·windows·python
hrhcode5 小时前
【LangGraph】四.持久化:保存和恢复执行状态
python·ai·langchain·agent·langgraph
xxyy8885 小时前
关于labelimg安装后在标注过程中闪退和死机的问题处理
开发语言·python
卷Java5 小时前
上下文压缩
开发语言·windows·python
AI技术增长5 小时前
Pytorch图像去噪实战(十二):DDPM图像去噪完整训练流程,构建可复现扩散模型工程
pytorch·python·深度学习
本地化文档5 小时前
setuptools-docs-l10n
python·github·gitcode
梦想不只是梦与想5 小时前
Python 属性访问的 MRO 规则
python·mro规则
Ulyanov6 小时前
基于 Python 的三维动态导弹攻防演示系统设计与实现:从架构到实战的深度剖析
开发语言·python·qt·架构·雷达电子对抗
Leinwin6 小时前
Claude 四月宕机七次:从一次事故看企业级 AI 部署的容灾设计
后端·python·flask
棉猴6 小时前
Python海龟绘图之绘制文本
javascript·python·html·write·turtle·海龟绘图·输出文本