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

Below is a code that will open a source file, and copy it to a template and then save the files as a new name. I would like to add multiple for loops to this script.

I have 27 files that I want to open, and paste to the template individually and save as different names each time.

I don't want to have to input the sources file name each time, and update the save each time. I'm not sure how to go about modifying the below script to make this work.


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

Paste source data one by one into template - then save each as

Example Save as:
Report_6a_v1_T01.xlsx
Report_6a_v1_T02.xlsx
Report_6a_v1_T03.xlsx
Report_6a_v1_P01.xlsx
Report_6a_v1_P02.xlsx
Report_6a_v1_P03.xlsx



import openpyxl as xl; 
import glob
import pandas as pd


  
# opening the source excel file 
filename ="C:/NNB-6a_v1_T01-Report.xlsx"
wb1 = xl.load_workbook(filename) 
ws1 = wb1.worksheets[1] 
  
# opening the destination excel file  
filename1 ="C:/Template.xlsx"
wb2 = xl.load_workbook(filename1) 
ws2 = wb2.worksheets[2] 
  
# calculate total number of rows and  
# columns in source excel file 
mr = ws1.max_row 
mc = ws1.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 = ws1.cell(row = i, column = j) 

        ws2.cell(row = i+12, column = j+1).value = c.value 
  
# saving the destination excel file 
wb2.save('C:/Report_6a_v1_T01.xlsx')
Reply
#2
Look into os.walk(), then could just use the original name with an exteion like backup or what ever attached to it.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
You could take some input to get a directory, and use os.listdir to get all the files in that directory. Look for files with a certain type of name and/or extension. You can also program it to make the destination file, the current library you're using might be able to do that but if not there are libraries that can make xlsx files. Hope this helps
Reply
#4
for letter in 'TP':
    for number in range(1, 4):
        print(f'NNB-6a_v1_{letter}{number:02}-Report.xlsx')
        print(f'Report_6a_v1_{letter}{number:02}.xlsx')
Output:
NNB-6a_v1_T01-Report.xlsx Report_6a_v1_T01.xlsx NNB-6a_v1_T02-Report.xlsx Report_6a_v1_T02.xlsx NNB-6a_v1_T03-Report.xlsx Report_6a_v1_T03.xlsx NNB-6a_v1_P01-Report.xlsx Report_6a_v1_P01.xlsx NNB-6a_v1_P02-Report.xlsx Report_6a_v1_P02.xlsx NNB-6a_v1_P03-Report.xlsx Report_6a_v1_P03.xlsx
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why both loops don't work in python? nau 3 1,091 Sep-21-2022, 02:17 PM
Last Post: rob101
  Why does Python not use parenthesis to contain loops? davidorlow 3 3,470 Jun-09-2021, 06:33 AM
Last Post: Gribouillis
  For loops in python Boinbo 3 78,978 Apr-18-2020, 01:23 AM
Last Post: buran
  For loops help, using python turtle SemiBeginnerPY 2 3,945 Mar-10-2020, 10:46 AM
Last Post: SemiBeginnerPY

Forum Jump:

User Panel Messages

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