Jul-15-2020, 08:16 PM
Using
enumerate
with a start value of 2 to change the row numberimport openpyxl as xl import os input_dir = 'C:\\work\\comparison\\NMN' template = 'C:\\work\\comparison\\template.xlsx' newFile = 'NNM_Comparison.xlsx' files = [file for file in os.listdir(input_dir) if os.path.isfile(file) and file.endswith(".xlsx")] wb2 = xl.load_workbook(template) ws2 = wb2.worksheets[0] for index, file in enumerate(files, 2): input_file = os.path.join(input_dir, file) wb1 = xl.load_workbook(input_file) ws = wb1.worksheets[0] ws2[f'A{index}'] = ws['A1'].value ws2[f'D{index}'] = ws['B4'].value ws2[f'E{index}'] = ws['D4'].value ws2[f'I{index}'] = ws['B5666'].value ws2[f'J{index}'] = ws['D5666'].value wb2.save(newFile)