多个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!')
相关推荐
踏着七彩祥云的小丑6 小时前
pytest——Mark标记
开发语言·python·pytest
不爱吃炸鸡柳7 小时前
Python入门第一课:零基础认识Python + 环境搭建 + 基础语法精讲
开发语言·python
Dxy12393102168 小时前
Python基于BERT的上下文纠错详解
开发语言·python·bert
SiYuanFeng9 小时前
Colab复现 NanoChat:从 Tokenizer(CPU)、Base Train(CPU) 到 SFT(GPU) 的完整踩坑实录
python·colab
炸炸鱼.10 小时前
Python 操作 MySQL 数据库
android·数据库·python·adb
_深海凉_11 小时前
LeetCode热题100-颜色分类
python·算法·leetcode
AC赳赳老秦11 小时前
OpenClaw email技能:批量发送邮件、自动回复,高效处理工作邮件
运维·人工智能·python·django·自动化·deepseek·openclaw
zhaoshuzhaoshu11 小时前
Python 语法之数据结构详细解析
python
AI问答工程师12 小时前
Meta Muse Spark 的"思维压缩"到底是什么?我用 Python 复现了核心思路(附代码)
人工智能·python
zfan52013 小时前
python对Excel数据处理(1)
python·excel·pandas