多个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!')
相关推荐
Birdy_x7 小时前
接口自动化项目实战(1):requests请求封装
开发语言·前端·python
我爱学习好爱好爱7 小时前
Ansible 常用模块详解:lineinfile、replace、get_url实战
linux·python·ansible
一轮弯弯的明月8 小时前
Python基础-速通秘籍(下)
开发语言·笔记·python·学习
千寻girling9 小时前
面试官 : “ 说一下 Python 中的常用的 字符串和数组 的 方法有哪些 ? ”
人工智能·后端·python
第一程序员10 小时前
Python基础学习路径:非科班转码者的入门指南
python·github
u01368638210 小时前
将Python Web应用部署到服务器(Docker + Nginx)
jvm·数据库·python
smchaopiao11 小时前
Python中字典与列表合并的问题与解决方法
开发语言·python
卡尔特斯11 小时前
Ultralytics YOLO26 自动对指定标注文件夹区分标注素材脚本与训练脚本
python·openai
2501_9216494911 小时前
期货 Tick 级数据与基金净值历史数据 API 接口详解
开发语言·后端·python·websocket·金融·区块链
njidf11 小时前
实战:用Python开发一个简单的区块链
jvm·数据库·python