How to Merge Multiple Excel Files into a Single Files with Python
Last Updated :
03 Jun, 2025
We often deal with multiple Excel files that need to be combined into a single consolidated file. Manually merging them or using Excel macros can be slow and prone to errors. To make this process faster and more reliable, we will use Python’s pandas module to automate merging multiple Excel files into one.
If you haven’t installed pandas yet, install it using:
pip install pandas
Using dataframe.append()
In this method, we first read all Excel files from a specified folder, then loop through each file to read its contents into a DataFrame. We then append each DataFrame one by one to a final DataFrame, which consolidates all the data. Finally, this combined DataFrame is exported to a new Excel file. This approach is simple to understand and works well for a small number of files.
Excel Files used are: FoodSales1-1, FoodSales2-1
Python
import glob
import pandas as pd
# specify folder path
path = "C:/downloads"
# get all .xlsx files in the folder
files = glob.glob(path + "/*.xlsx")
# read each file and store in list
li = []
for f in files:
li.append(pd.read_excel(f))
# initialize empty dataframe for final output
df_final = pd.DataFrame()
# append each DataFrame one-by-one
for df in li:
df_final = df_final.append(df, ignore_index=True)
# write the final DataFrame to a new Excel file
df_final.to_excel('total_food_sales.xlsx', index=False)
Output :
dataframe.append()Explanation:
- glob.glob() lists all Excel files in the folder.
- We store each file's data in a list li.
- df_final.append(df) stacks all rows together.
- The result is written to total_food_sales.xlsx.
Using pandas.concat()
This method reads all the Excel files into a list of DataFrames and then merges them in a single step using concat(). This avoids the overhead of appending DataFrames one at a time and is more efficient, especially when working with a large number of files. The merged DataFrame is then saved to a new Excel file
Example:
In the last example, we worked on only two Excel files with a few rows. Let's try merging more files each containing approximately 5000 rows and 7 columns. We have 5 files BankE, BankD, BankC, BankB, BankA having historical stock data for respective bank. Let's merge them into a single 'Bank_Stocks.xlsx' file. Here we are using the pandas.concat() method.
Python
import glob
import pandas as pd
# specify folder path
path = "C:/downloads"
# get all .xlsx files in the folder
files = glob.glob(path + "/*.xlsx")
# use list comprehension to read all files
a = [pd.read_excel(f) for f in files]
# concatenate all DataFrames together
df_final = pd.concat(a, ignore_index=True)
# export to a single Excel file
df_final.to_excel('Bank_Stocks.xlsx', index=False)
Output :
BankstocksExplanation:
- All files are read and stored in list a.
- pd.concat() merges them efficiently into df_final.
- The combined output is saved as Bank_Stocks.xlsx.
Relevant Articles
Similar Reads
How to merge multiple excel files into a single files with Python ? We often deal with multiple Excel files that need to be combined into a single consolidated file. Manually merging them or using Excel macros can be slow and prone to errors. To make this process faster and more reliable, we will use Pythonâs pandas module to automate merging multiple Excel files in
3 min read
How to Merge Multiple Excel Files into one Folder? Power Query is a data manipulation tool frequently used for business intelligence and data analysis. Both Microsoft Excel and Microsoft Power BI support Power Query. A single source of truth and well-organized, error-free data are requirements for high-quality analysis. While many analysts spend man
4 min read
How to Merge multiple CSV Files into a single Pandas dataframe ? While working with CSV files during data analysis, we often have to deal with large datasets. Sometimes, it might be possible that a single CSV file doesn't consist of all the data that you need. In such cases, there's a need to merge these files into a single data frame. Luckily, the Pandas library
3 min read
How to merge multiple folders into one folder using Python ? In this article, we will discuss how to move multiple folders into one folder. This can be done using Python's OS and Shutil module. Approach:Get the current directory and the list of the folders you want to merge.Loop through the list of folders and store their content in a list. Here, we have stor
4 min read
How to Merge all excel files in a folder using Python? In this article, we will see how to combine all Excel files present in a folder into a single file. Module used: The python libraries used are: Pandas: Pandas is a python library developed for a python programming language for manipulating data and analyzing the data. It is widely used in Data Scien
3 min read
Python | Write multiple files data to master file Given a number of input files in a source directory, write a Python program to read data from all the files and write it to a single master file. Source directory contains n number of files, and structure is same for all files. The objective of this code is to read all the files one by one and then
2 min read