Ниже приведен код, который открывает исходный файл и копирует его в шаблон, а затем сохраняет файлы под новым именем. Я хотел бы добавить в этот скрипт несколько циклов for.
У меня есть 27 файлов, которые я хочу открыть, вставить в шаблон индивидуально и каждый раз сохранять под разными именами.
Я не Я не хочу каждый раз вводить имя исходного файла и каждый раз обновлять сохранение. Я не уверен, как go изменить приведенный ниже сценарий, чтобы это работало.
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')