Jun-16-2020, 09:03 PM
Hello,
I'm trying to get this script to loop through 27 excel files (source), and paste them one by one to my template file, and then save to an output folder once finished I will have 27 new files inside my output folder.
Examples Source Files:
NNB-6a_v1_T01-Report.xlsx
NNB-6a_v1_T02-Report.xlsx
NNB-6a_v1_T03-Report.xlsx
NNB-6a_v1_P01-Report.xlsx
NNB-6a_v1_P02-Report.xlsx
NNB-6a_v1_P03-Report.xlsx
When I run this script it for some reason only grabs file 26, and paste it to my template, and saves it to my output folder. I don't know why id decides to start at file 26 and not a the first excel file.
I'm trying to get this script to loop through 27 excel files (source), and paste them one by one to my template file, and then save to an output folder once finished I will have 27 new files inside my output folder.
Examples Source Files:
NNB-6a_v1_T01-Report.xlsx
NNB-6a_v1_T02-Report.xlsx
NNB-6a_v1_T03-Report.xlsx
NNB-6a_v1_P01-Report.xlsx
NNB-6a_v1_P02-Report.xlsx
NNB-6a_v1_P03-Report.xlsx
When I run this script it for some reason only grabs file 26, and paste it to my template, and saves it to my output folder. I don't know why id decides to start at file 26 and not a the first excel file.
import openpyxl as xl; import os files = [file for file in os.listdir('C:/data') if os.path.isfile(file) and file.endswith('.xlsx')] output = ('C:/data/output') for file in files: wb=xl.load_workbook(file) ws=wb.worksheets[1] # Open template Template ="C:/data/Template.xlsx" wb2 = xl.load_workbook(Template) ws2 = wb2.worksheets[2] # calculate total number of rows and # columns in source excel file mr = ws.max_row mc = ws.max_column # copying the cell values from source # excel file to destination excel file for i in range (1, mr + 1): for j in range (1, mc + 1): # reading cell value from source excel file c = ws.cell(row = i, column = j) # Cells for source data to pasted inside Template ws2.cell(row = i+12, column = j+1).value = c.value # saving the destination excel file wb2.save('./output/'+file+'.xlsx')