Jun-16-2020, 12:39 PM
Hello,
Below is a code that will open a source file, and copy it to a template and then save the files as a new name. I would like to add multiple for loops to this script.
I have 27 files that I want to open, and paste to the template individually and save as different names each time.
I don't want to have to input the sources file name each time, and update the save each time. I'm not sure how to go about modifying the below script to make this work.
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
Paste source data one by one into template - then save each as
Example Save as:
Report_6a_v1_T01.xlsx
Report_6a_v1_T02.xlsx
Report_6a_v1_T03.xlsx
Report_6a_v1_P01.xlsx
Report_6a_v1_P02.xlsx
Report_6a_v1_P03.xlsx
Below is a code that will open a source file, and copy it to a template and then save the files as a new name. I would like to add multiple for loops to this script.
I have 27 files that I want to open, and paste to the template individually and save as different names each time.
I don't want to have to input the sources file name each time, and update the save each time. I'm not sure how to go about modifying the below script to make this work.
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
Paste source data one by one into template - then save each as
Example Save as:
Report_6a_v1_T01.xlsx
Report_6a_v1_T02.xlsx
Report_6a_v1_T03.xlsx
Report_6a_v1_P01.xlsx
Report_6a_v1_P02.xlsx
Report_6a_v1_P03.xlsx
import openpyxl as xl; import glob import pandas as pd # opening the source excel file filename ="C:/NNB-6a_v1_T01-Report.xlsx" wb1 = xl.load_workbook(filename) ws1 = wb1.worksheets[1] # opening the destination excel file filename1 ="C:/Template.xlsx" wb2 = xl.load_workbook(filename1) ws2 = wb2.worksheets[2] # calculate total number of rows and # columns in source excel file mr = ws1.max_row mc = ws1.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 = ws1.cell(row = i, column = j) ws2.cell(row = i+12, column = j+1).value = c.value # saving the destination excel file wb2.save('C:/Report_6a_v1_T01.xlsx')