2

I have several workbooks, each with three sheets. I want to loop through each workbook and merge all the data from sheet_1 into a new workbook_1 file, sheet_2 into workbook_2 file & sheet_3 into workbook_3.

As far as I can tell the script below does everything I need, except rather than appending the data, it overwrites the data from the previous iteration.

For the sake of parsimony I've shortened, cleaned & simplified my script, but I'm happy to share the full script if needed.

import pandas as pd
import glob

search_dir= ('/Users/PATH/*.xlsx')

sheet_names = ['sheet_1','sheet_2','sheet_2']

def a_joiner(sheet):
    for loop_x in glob.glob(search_dir):   
    try:

        if sheet == 'sheet_1':
            id_file= pd.ExcelFile(loop_x)                            
            df_1 = id_file.parse(sheet, header= None)             
            writer= pd.ExcelWriter('/Users/PATH/%s.xlsx' %(sheet), engine= 'xlsxwriter')                         
            df_1.to_excel(writer)                    
            writer.save()

        elif sheet == 'sheet_2':
           #do same as above

        else:
           #and do same as above again

    except Exception as e:
       print('Error:',e)

for sheet in sheet_names:
    a_joiner(sheet)
chrisaycock
  • 36,470
  • 14
  • 88
  • 125
SeánMcK
  • 392
  • 3
  • 17

1 Answers1

0

You can also easilly append data like:

df = []
for f in ['c:\\file1.xls', 'c:\\ file2.xls']:
    data = pd.read_excel(f, 'Sheet1').iloc[:-2]
    data.index = [os.path.basename(f)] * len(data)
    df.append(data)

df = pd.concat(df)

From: Using pandas Combining/merging 2 different Excel files/sheets

Community
  • 1
  • 1
Destrif
  • 2,104
  • 1
  • 14
  • 22