Python 合并同一文件夹下所有Excel_xlsx文件

转载:How to Consolidate Multiple Excel Workbooks With Python
想要将同一文件夹下xlsx文件合并为一个文件:
在这里插入图片描述
代码:代码来源(仅供学习)

import pandas as pd
import os
input_file_path = "./excel/"
output_file_path = "./excel/"

#create a list to store all the file references of the input folder using the listdir function from the os library.
#To see the contents of a library (like the listdir function, you can use the dir function on the library name).
#Use dir(library_name) to list contents

excel_file_list = os.listdir(input_file_path)

#create a new, blank dataframe, to handle the excel file imports
df = pd.DataFrame()

#Run a for loop to loop through each file in the list
for excel_files in excel_file_list:
   #check for .xlsx suffix files only
   if excel_files.endswith(".xlsx"):
       #create a new dataframe to read/open each Excel file from the list of files created above
       df1 = pd.read_excel(input_file_path+excel_files)
       #append each file into the original empty dataframe
       df = df.append(df1)

#transfer final output to an Excel (xlsx) file on the output path
df.to_excel(output_file_path+"merge.xlsx",index=None)
print('done!')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值