Home > Software engineering >  Merge excel files from 2 folders with output to another folder
Merge excel files from 2 folders with output to another folder

Time:08-01

i have 3 xlsx files in 2 subfolders of main folder. is there a way in python to take check same name files from both folders and combine into new excel file with outputs from both in different tabs (tab names should be folder name)

Input:

Desktop\main_folder\folder_a\test1.xlsx  
Desktop\main_folder\folder_a\test2.xlsx  
Desktop\main_folder\folder_a\test3.xlsx

Desktop\main_folder\folder_b\test1.xlsx  
Desktop\main_folder\folder_b\test2.xlsx  
Desktop\main_folder\folder_b\test3.xlsx  

Output:

Desktop\main_folder\folder_c\test1.xlsx

(sheet 1 : data from folder a [sheet name folder_a])
(sheet 2 : data from folder b [sheet name folder_b])

code

CodePudding user response:

Yes, you can use os.listdir() to list all files in your folder_a and folder_b and collect those file names into a list. You can later create a loop to check for file name that exist in both folder. Then use Python Excel modules such as openpyxl or XlsxWriter to create new worksheet in the existing excel workbook and move the excel worksheet from another workbook to the new worksheet.

  • Related