Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python loop problem
#1
Hello,

I'm trying to get this script to loop through 27 excel files (source), and paste them one by one to my template file, and then save to an output folder once finished I will have 27 new files inside my output folder.

Examples Source Files:
NNB-6a_v1_T01-Report.xlsx
NNB-6a_v1_T02-Report.xlsx
NNB-6a_v1_T03-Report.xlsx
NNB-6a_v1_P01-Report.xlsx
NNB-6a_v1_P02-Report.xlsx
NNB-6a_v1_P03-Report.xlsx

When I run this script it for some reason only grabs file 26, and paste it to my template, and saves it to my output folder. I don't know why id decides to start at file 26 and not a the first excel file.


import openpyxl as xl; 
import os

files = [file for file in os.listdir('C:/data') if os.path.isfile(file) and file.endswith('.xlsx')]


output = ('C:/data/output')



for file in files:
    wb=xl.load_workbook(file)
    ws=wb.worksheets[1]
    
# Open template
Template ="C:/data/Template.xlsx"
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 
wb2.save('./output/'+file+'.xlsx')
Reply


Messages In This Thread
Python loop problem - by Kristenl2784 - Jun-16-2020, 09:03 PM
RE: Python loop problem - by buran - Jun-17-2020, 04:09 AM
RE: Python loop problem - by Kristenl2784 - Jun-17-2020, 01:01 PM
RE: Python loop problem - by buran - Jun-17-2020, 01:16 PM
RE: Python loop problem - by Kristenl2784 - Jun-17-2020, 05:28 PM
RE: Python loop problem - by buran - Jun-17-2020, 05:56 PM
RE: Python loop problem - by Kristenl2784 - Jun-17-2020, 07:02 PM
RE: Python loop problem - by buran - Jun-17-2020, 07:05 PM
RE: Python loop problem - by Kristenl2784 - Jun-17-2020, 07:25 PM
RE: Python loop problem - by buran - Jun-17-2020, 07:49 PM
RE: Python loop problem - by Kristenl2784 - Jun-18-2020, 03:52 PM
RE: Python loop problem - by buran - Jun-18-2020, 07:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  While Loop Problem Benno2805 1 601 Sep-06-2023, 04:51 PM
Last Post: deanhystad
  Loop reading csv file problem faustineaiden 1 1,599 Dec-11-2021, 08:40 AM
Last Post: ibreeden
  Infinite loop problem Zirconyl 5 3,049 Nov-16-2020, 09:06 AM
Last Post: DeaD_EyE
  Dataframe mean calculation problem: do we have to loop? sparkt 1 2,199 Aug-28-2020, 02:41 PM
Last Post: sparkt
  Problem with append list in loop michaelko03 0 1,710 Feb-16-2020, 07:04 PM
Last Post: michaelko03
  problem with for loop using integers python_germ 5 3,072 Aug-31-2019, 11:42 AM
Last Post: jefsummers
  problem in loop roseojha 3 2,335 Aug-26-2019, 09:03 AM
Last Post: perfringo
  Nested while loop problem + turtle DreamingInsanity 3 3,018 Jul-06-2019, 02:01 PM
Last Post: DreamingInsanity
  Problem Passing Arguement to do loop stephenmolnar 10 4,930 May-13-2019, 02:56 PM
Last Post: Gribouillis
  Nested for loop strange problem mcva 2 2,660 Mar-16-2019, 12:53 PM
Last Post: mcva

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020