python_合并同一个文件夹下的excel文件

python_合并同一个文件夹下的excel文件

import os
import glob
import pandas as pd

def merge_excel_sheets(input_folder, output_file):
    # 创建一个空的 DataFrame 用于存储所有数据
    combined_data = pd.DataFrame()

    # 获取指定文件夹内所有的 Excel 文件
    excel_files = glob.glob(os.path.join(input_folder, '*.xlsx'))

    # 循环遍历每一个 Excel 文件
    for file in excel_files:
        # 读取每个文件的 Sheet1,并跳过表头行
        data = pd.read_excel(file, sheet_name='Sheet1', engine='openpyxl')
        
        # 将当前文件的数据添加到总的 DataFrame 中
        combined_data = pd.concat([combined_data, data], ignore_index=True)
        
    # 写入新的 Excel 文件
    combined_data.to_excel(output_file, index=False, engine='openpyxl')

# 指定输入文件夹和输出文件名
input_folder = 'D:\\desktop\\新建文件夹'
output_file = 'D:\\desktop\\combined_output.xlsx'

# 调用函数
merge_excel_sheets(input_folder, output_file)
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值