Я хотел бы, чтобы этот скрипт выполнял пару вещей:
Как мне сделать так, чтобы я мог просматривать определенный диапазон файлов Excel. Возможно, я не хочу, например, l oop через все 27 файлов, возможно, я хочу только l oop через файлы 10-20 или 5-10, например. Прямо сейчас сценарий просматривает все файлы Excel, которые у меня есть в папке ввода. Иногда я действительно хочу пройти через все oop, но бывают случаи, когда я просто хочу oop через определенный их диапазон.
Я хочу сохранить файлы под другим именем. Сейчас файлы excel называются: NNB-2a_v03_Nm-Report, NNB-2a_v03_T01-Report, NNB-2a_v03_T02-Report
Я хочу сохранить их как: Report-Nm, Report- T01, Отчет-T02
import openpyxl as xl;
import os
import pandas as pd
input_dir = 'C:/data'
output_dir = os.path.join(input_dir, 'output')
template = 'C:/data/Template.xlsx'
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 = os.path.join(output_dir, file)
wb2.save(output_file)