简体   繁体   中英

Looping through a folder to merge several excel sheets into one column

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM