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

This script currently loops through all the excel files in my input dir. How would I make it so I could pick excel files 5-10, or 15-20 to loop through instead of all the files. Sometimes I don't need to loop through them all.


import openpyxl as xl; 
import os

 
input_dir = 'C:/data'
output_dir = os.path.join(input_dir, 'output') 
template = 'C:/data/Template.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) # make the full path, so that it does not depend on input_dir and CWD being the same
    wb=xl.load_workbook(input_file)
    ws=wb.worksheets[1]
      
    # Open template
    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 

    output_file = os.path.join(output_dir, file)
    wb2.save(output_file)
Reply
#2
With no actual example of what the filename would look like all I can advise is the following.
Split the file pathnames to separate out the numbers, convert them from strings to numbers then check they are in the required range.
Reply
#3
(Jun-18-2020, 04:38 PM)Yoriz Wrote: With no actual example of what the filename would look like all I can advise is the following.
Split the file pathnames to separate out the numbers, convert them from strings to numbers then check they are in the required range.

Example of the excel files inside the directory
NNB-6a_v1_Nm-Report.xlsx
NNB-6a_v1_T01-Report.xlsx
NNB-6a_v1_T02-Report.xlsx
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [loop] Exclude ranges in… range? Winfried 2 1,453 May-14-2023, 04:29 PM
Last Post: Winfried
  How to loop through all excel files and sheets in folder jadelola 1 4,490 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  export into excel, how to implement pandas into for-loop deneme2 6 2,452 Sep-01-2022, 05:44 AM
Last Post: deneme2
  matplotlib x axis range goes over the set range Pedroski55 5 3,209 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Using Excel Cell As A Variable In A Loop knight2000 7 4,120 Aug-25-2021, 12:43 PM
Last Post: snippsat
  Using Excel Cell As A Variable In A Loop knight2000 7 5,020 Jul-18-2021, 10:52 AM
Last Post: knight2000
  Python “Formula” Package: How do I parse Excel formula with a range of cells? JaneTan 1 2,680 Jul-12-2021, 11:09 AM
Last Post: jefsummers
  how to reference excel range one by one mqzhang99 1 1,659 Sep-12-2020, 04:31 AM
Last Post: buran
  Mirror cell range in excel TRaWolTa89 0 1,925 Apr-07-2020, 06:25 PM
Last Post: TRaWolTa89
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 7,055 Jan-24-2020, 06:19 AM
Last Post: KiNeMs

Forum Jump:

User Panel Messages

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