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的数据进行合并
相关推荐
Cloud_Shy6181 小时前
解读《Effective Python 3rd Edition》:从练气到老魔(第五章 Item 33 - 35)
开发语言·人工智能·笔记·python·学习方法
abcy0712131 小时前
python pandas csv异步后台清洗前端优先返回成功信息
前端·python·pandas
颜酱2 小时前
LangChain使用RAG 入门:让大模型读懂你的私有文档
python·langchain
天天进步20152 小时前
Python全栈项目--校园智能宿舍管理系统
开发语言·python
测试员周周2 小时前
【AI测试智能体-面试】AI测试面试60题(附回答思路)
人工智能·python·功能测试·测试工具·单元测试·自动化·测试用例
用户8356290780513 小时前
使用 Python 操作 Word 评论和回复
后端·python
Zella折耳根3 小时前
复习篇-继承和接口
java·开发语言·python
诗词在线3 小时前
求推荐飞花令
大数据·人工智能·python
yijianace3 小时前
Python线程与多线程完全总结(从入门到理解并发本质)
开发语言·python
会Tk矩阵群控的小木4 小时前
基于Python的iMessage短信群发与社媒多账号统一管理系统实现
开发语言·windows·python·新媒体运营·开源软件·个人开发