Python+Pandas批量合并大量excel文件

requirments.txt

et-xmlfile==1.1.0
numpy==1.24.3
openpyxl==3.1.2
pandas==2.0.1
python-dateutil==2.8.2
pytz==2023.3
six==1.16.0
tzdata==2023.3

main.py


import os
import pandas as pd

dir_path = os.path.dirname(os.path.abspath(__file__))
source_location = os.path.join(dir_path,"source")

df = []
target = []
i = 0 
for filename in os.listdir(source_location): 
    if filename.endswith('.xlsx'):
        df.append(filename)
    

for i in range (0,len(df)):
# for i in range (0,2):
    excel_file_location = os.path.join(source_location,df[i])
    print(excel_file_location)
    dfs = pd.read_excel(excel_file_location)
    dfs.insert(loc=18,column='file_name',value=df[i])
    target.append(dfs)
    i = i +1
df_concat = pd.concat(target)
df_concat.to_excel(dir_path+'/{}.xlsx'.format('合并结果'),index=None)


df_concat.to_csv('df_concat.csv',sep=',',header=True, encoding='utf_8_sig', index=False)
print(i)

excel文件放在source目录下

posted on   BionExit  阅读(85)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
历史上的今天:
2021-05-05 python通过递归清除字典中value为特定值的key

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示