Home > front end >  How to merge multiple excel file via python
How to merge multiple excel file via python

Time:01-17

I have 30 excel files and want to combine them all into only 1 excel file (using Python) and i want only 1 header on top of file ( not keep happening 30 times)

dont know how to write in python please help.

thank you so much

CodePudding user response:

This is my code snippet for merging 13 excel file in to 1 file.

fout=open("1300 restaurant data.csv", "a", encoding="utf8")

# now the rest:
for num in range(1,13):
    f = open(str(num) ".csv",encoding="utf8")
    for line in f:
         fout.write(line)
    f.close() # not really needed
fout.close()

CodePudding user response:

Try using the below code to merge a list of files into a single file.

import glob
import pandas as pd

path = "C:/documents"
file_list = glob.glob(path   "/*.xlsx")
excel_list = []
  
for file in excel_list:
    excel_list.append(pd.read_excel(file))
excel_merged = pd.DataFrame()
  
for excel_file in excel_list:    
    excel_merged = excel_merged.append(
      excel_file, ignore_index=True)
excel_merged.to_excel('mergedFile.xlsx', index=False)
  •  Tags:  
  • Related