Внутри моего входного каталога у меня есть три отчета FinalReport_Nm, FinalReport_S01, FinalReport_S02 и c. Я добавлю к этому еще около 50 отчетов, так что именование продолжится с S03, S04, T01, T02 и c. Я хочу, чтобы этот сценарий сделал l oop через папку отчетов, взял FinalReport_NM и вставил его в мой шаблон, а затем сохранил его как SecondaryReport_1a_NM, а затем l oop обратно и скопировал FinalReport_S01, вставил его в шаблон и сохраните как SecondaryReport_1a_S01 et c.
Я, хотя, создав расписания NM S01 S02, как показано ниже в скрипте, и пытаясь объединить внизу, где говорится, что output_file будет работать, но это огромный потерпеть поражение. Как я могу заставить этот скрипт работать, когда он будет переименовывать файлы, просматривая их.
import openpyxl as xl;
import os
input_dir = 'C:\\Python\\Reports'
output_dir = 'C:\\Reports\\output'
template = 'C:\\Python\\Report_Template.xlsx'
NewFileName = 'SecondaryReport_1a_'
schedule_index = 0
schedules=['Nm', 'S01', 'S02']
files = [file for file in os.listdir(input_dir)
if os.path.isfile(file) and file.endswith('.xlsx')]
for file in files:
input_file = os.path.join(input_dir, file)
wb=xl.load_workbook(input_file)
ws=wb.worksheets[1]
# Open template
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
output_file = (output_dir, f"{summaryFile}_{schedules[schedule_index]}")
schedule_index += 1
wb2.save(output_file)