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的数据进行合并
相关推荐
冷雨夜中漫步2 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
郝学胜-神的一滴3 小时前
深入解析Python字典的继承关系:从abc模块看设计之美
网络·数据结构·python·程序人生
百锦再3 小时前
Reactive编程入门:Project Reactor 深度指南
前端·javascript·python·react.js·django·前端框架·reactjs
喵手5 小时前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
2501_944934735 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
helloworldandy5 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
肖永威6 小时前
macOS环境安装/卸载python实践笔记
笔记·python·macos
TechWJ6 小时前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto
枷锁—sha7 小时前
【SRC】SQL注入WAF 绕过应对策略(二)
网络·数据库·python·sql·安全·网络安全
abluckyboy7 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法