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

I'm trying to use an excel file that has a list of names in it, that I want the script to use when doing a save as at the end. The list is found in OutputNames_List directory, and the name of the file is outputFileNames.xlsx as shown below in the script. What I currently have at the bottom of the script is not working.

import openpyxl as xl; 
import os
import pandas as pd
 
input_dir = 'C:data'
output_dir = os.path.join(input_dir, 'output') 
template = 'C:/data/template/Template.xlsx'
OutputNames_list = 'C:/data/outputNames'
OutputNames = pd.read_excel(OutputNames_list+'outputFileNames.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, OutputNames['filename'])
    output_detailed = pd.ExcelWriter(output_file)

    
    wb2.save(output_file)
Reply
#2
>>> import os
>>> 
>>> input_dir = 'C:data' # There is a problem here
>>> output_dir = os.path.join(input_dir, 'output') 
>>> output_dir
'C:data\\output' 
>>> 
>>> # Fix
>>> input_dir = 'C:\\data'
>>> output_dir = os.path.join(input_dir, 'output') 
>>> output_dir
'C:\\data\\output'
Reply
#3
(Jun-17-2020, 03:48 PM)snippsat Wrote:
>>> import os
>>> 
>>> input_dir = 'C:data' # There is a problem here
>>> output_dir = os.path.join(input_dir, 'output') 
>>> output_dir
'C:data\\output' 
>>> 
>>> # Fix
>>> input_dir = 'C:\\data'
>>> output_dir = os.path.join(input_dir, 'output') 
>>> output_dir
'C:\\data\\output'
I fixed the data part to input_dir = 'C:/data'

This still doesn't fix the issue I'm having when trying to do a save as. The script runs fine till the save part where I want to use the excel file "outputnames" which has a list of names I want to use. I don't want it to save with the same file name that initially get's read in. So I have a list in an excel file that has the names that I want each file to be saved as.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem using exec to save local variables dkarl 0 430 Dec-01-2019, 08:52 AM
Last Post: dkarl

Forum Jump:

User Panel Messages

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