多个excel的sheet合并到一个excel下

目标:多个excel的sheet合并到一个excel下(不同sheet)

要求:原始数据不同excel中的sheet名不同

python 复制代码
import pandas as pd
import os

# 多个Excel文件所在的文件夹路径
folder_path = r"D:\data\sheet"

# 输出合并后的Excel文件路径
output_path = r"D:\data\合并.xlsx"

# 获取文件夹中所有Excel文件的文件名
file_names = [f for f in os.listdir(folder_path) if f.endswith('.xlsx')]

# 使用with语句创建Excel写入对象
with pd.ExcelWriter(output_path) as writer:
    # 遍历每个Excel文件
    for file_name in file_names:
        # 读取Excel文件
        file_path = os.path.join(folder_path, file_name)
        df = pd.read_excel(file_path, sheet_name=None)

        # 遍历每个Sheet
        for sheet_name, sheet_data in df.items():
            # 将当前Sheet写入目标Excel文件的不同Sheet
            sheet_data.to_excel(writer, sheet_name=sheet_name, index=False)

print('Success!')
相关推荐
SPC的存折几秒前
Python3编程之python基础
开发语言·python
2401_873204656 分钟前
Python面向对象编程(OOP)终极指南
jvm·数据库·python
XW010599921 分钟前
6-函数-1 使用函数求特殊a串数列和
数据结构·python·算法
m0_5698814729 分钟前
使用Python进行网络设备自动配置
jvm·数据库·python
波诺波33 分钟前
项目pid-control-simulation-main 中的 main.py 代码讲解
开发语言·python
巧妹儿1 小时前
Python 配置管理封神技:pydantic_settings+@lru_cache,支持优先级,安全又高效,杜绝重复加载!
开发语言·python·ai·配置管理
独隅1 小时前
Python AI 全面使用指南:从数据基石到智能决策
开发语言·人工智能·python
胡耀超1 小时前
Web Crawling 网络爬虫全景:技术体系、反爬对抗与全链路成本分析
前端·爬虫·python·网络爬虫·数据采集·逆向工程·反爬虫
小陈的进阶之路1 小时前
Selenium元素定位
python·selenium