Python合并多个Excel文件

 时间:2026-02-14 14:05:03

1、导入包

import os

import pandas as pd

import openpyxl

import warnings

warnings.filterwarnings("ignore")

Python合并多个Excel文件

2、添加源宿脂蚂文件地址,获取文件名

dir = "./源文件"

filenames = os.listdir(dir) 

print(filenames)

Python合并多个Excel文件

Python合并多个Excel文件

3、新增空list,遍历文件append到空列

dfs = []

df1 = []

for name in filenames:

    print(name)

    df1 = pd.read_excel(os.path.join(dir, name))

    username = name

    df1['文件名'] = username

    dfs.append(df1)

Python合并多个Excel文件

4、合并彩祝文件名列,表示数据来源,后保存合并后的文件

df = pd.concat(dfs,ignore_index=True)

df.to_excel('./合并文件.xlsx'迅况, index=False)

Python合并多个Excel文件

Python合并多个Excel文件

  • Excel一键为非空单元格添加边框技巧
  • Excel怎么快速定位至指定工作表?
  • WPS表格中如何根据行、列号返回单元格地址
  • VBA中如何遍历worksheet并删除特定sheet
  • Excel中连接多个单元格内容同时忽略空单元格
  • 热门搜索
    沈从文简介及作品 小于等于号怎么打 中兴手机大全 危险期怎么算 中考作文大全 汽车报价大全官网 小鸟简笔画图片大全 身上很痒怎么办 尿道感染怎么治疗 纪录片大全