Jul-15-2020, 03:48 PM
Hello,
How would I increment ws2 cells? ws cells can remain the same because when the script loops through my files it will copy data from the same cells of each file. ws2 cells are inside my template so if I don't increment them the data is just written over. For instance under the template section it says ws2['A2']=ws['A1'] this means the data will write to ws2 cell A2 but the next loop will need to write to ws2 cell A3 so the data isn't written over.
How would I increment ws2 cells? ws cells can remain the same because when the script loops through my files it will copy data from the same cells of each file. ws2 cells are inside my template so if I don't increment them the data is just written over. For instance under the template section it says ws2['A2']=ws['A1'] this means the data will write to ws2 cell A2 but the next loop will need to write to ws2 cell A3 so the data isn't written over.
import 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")] for file in files: input_file = os.path.join(input_dir, file) wb1=xl.load_workbook(input_file) ws=wb1.worksheets[0] wb2 = xl.load_workbook(template) ws2 = wb2.worksheets[0] ws2['A2']=ws['A1'] ws2['D2']=ws['B4'] ws2['E2']=ws['D4'] ws2['I2']=ws['B5666'] ws2['J2']=ws['D5666'] output_file = (newFile) wb2.save(output_file)