Increment cells copied inside template Python

How would I go about making it so that ws2 cells increment? Right now when the script loops through the files in my input_dir it just overwrites the data that I paste into ws2 cells. ws1 cells don’t need to incremented because it’s the same data copied out of each file. But ws2 is my template, and I need the cells to increment. An example of what I mean is below. I’m guessing there is an easier way to make this happen instead of having to set it up like my example.

Example: ws2['A2']=ws1['A1'].value[28:] (File 1) ws2['A3']=ws1['A1'].value[28:] (File 2) ws2['A4']=ws1['A1'].value[28:] (File 3)   import openpyxl as xl;  import os   input_dir = 'C:\\\\work\\comparison\\NNM' template = 'C:\\work\\comparison\\template.xlsx' Summary = 'C:\\work\\comparison\\Summary.xlsx' newFile = '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)     ws1=wb1.worksheets[0]     ws1['A1'].value     ws1['B4'].value     ws1['D4'].value     ws1['B5666'].value     ws1['D5666'].value     ws1['E5666'].value      wb2 = xl.load_workbook(template)      ws2 = wb2.worksheets[0]      ws2['A2']=ws1['A1'].value[28:]     ws2['D2']=ws1['B4'].value     ws2['E2']=ws1['D4'].value     ws2['I2']=ws1['B5666'].value     ws2['J2']=ws1['D5666'].value     ws2['O2']=ws1['E5666'].value                  output_file = (newFile)     wb2.save(output_file) 
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.