Open In App

How to Merge Multiple Excel Files into a Single Files with Python

Last Updated : 03 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 :

foodsale3
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 :

bankstocks
Bankstocks

Explanation:

  • 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


Article Tags :
Practice Tags :

Similar Reads