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

Right now when this script saves a file it uses the summaryFile variable 'Report_v1_' and then adds the first schedule name 'Nm' to the end so it saves as Report_v1_Nm, and then it loops around and saves the next as Report_v1_S01.

How would I make it so that if I wanted to start saving at 'S01' I can, instead of 'Nm'? If I change the schedule_index from 0 to 1, it does what I want but, it takes the first report in the input dir which is finalstats_nm, and saves it as Report_v1_S01 so it has "Nm" data saved under S01 which is not right. If I want to start at S01, I want it to grab the finalstats_S01 file from the input_dir, and save as Report_v1_S01.


import openpyxl as xl; 
import os
 
input_dir = 'C:\\Python\\Reports Combined'
output_dir = 'C:\\Python\\Reports Combined\\output'
template = 'C:\\Python\\Template.xlsx'
summaryFile = 'Report_v1_'

schedule_index = 0
schedules=['Nm', 'S01', 'S02','S03','S04','S05',S06']
  
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)
    wb=xl.load_workbook(input_file)
    ws=wb.worksheets[1]
      
    # Open template
    wb2 = xl.load_workbook(template) 
    ws2 = wb2.worksheets[1] 
     
    # 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+8, column = j+1).value = c.value 
     
    # saving the destination excel file 
	
    output_file =os.path.join (output_dir, f"{summaryFile}_{schedules[schedule_index]}.xlsx")
    schedule_index += 1
    wb2.save(output_file)
Reply
#2
I'm not sure what you're asking. It starts with 'Nm' because that's the first element in the schedules list on line 10. If you remove the 'Nm' element, it will instead start with 'S01'. Is that what you want?

Also, there appears to be a problem with that line because the element doesn't have quote marks on both sides.
Reply
#3
(Jul-13-2020, 03:48 PM)bowlofred Wrote: I'm not sure what you're asking. It starts with 'Nm' because that's the first element in the schedules list on line 10. If you remove the 'Nm' element, it will instead start with 'S01'. Is that what you want?

Also, there appears to be a problem with that line because the element doesn't have quote marks on both sides.

I explained this poorly. How would I make it so that the for loop file in files is set to a range? Like maybe I want the script to start off on file 3 in my input_dir, instead of starting at the first file?
Reply
#4
The problem is that instead of looping over some particular names, it's looping over the files in the directory, and those names are (currently) in that particular order. So if the filenames ever changed, this would break.

Better would be to have the two synchronized to the same list. Only open the files that match the names.

As a hack, you could continue if the index equal zero. But the current structure is brittle and depends on the directory entries matching a list in the code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  extract zip from .msg files and saving according to date stamp of email natjo 3 2,926 Aug-13-2020, 09:35 PM
Last Post: bowlofred
  Downloading And Saving Zip Files To A Particular Path Folder eddywinch82 2 2,549 Jan-06-2020, 07:56 PM
Last Post: eddywinch82
  Python 3.6.5 - Saving files Mradr 1 2,880 Jul-22-2018, 12:21 AM
Last Post: Larz60+
  openpyxl and saving xlsm files Patrick 2 19,359 Mar-15-2018, 06:15 PM
Last Post: Patrick
  reading .xls files and saving as .xlsx jon0852 1 6,878 Oct-17-2017, 08:32 PM
Last Post: buran
  openpyxl saving files issue Pedroski55 1 6,678 Sep-16-2017, 02:05 AM
Last Post: Pedroski55
  saving my *.py files tozqo 2 3,582 Jun-21-2017, 07:01 AM
Last Post: Kebap

Forum Jump:

User Panel Messages

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