Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to pass filename to ExcelWriter

I am trying to pass a customized filename variable to ExcelWriter but cannot get the ExcelWriter portion of this to work for some reason. If I replace "Sheetname" with "Temp.xlsx" in the ExcelWriter function this works, but i'm not able to pass my variable to the function. I need to be able to store today's date in the filename every time the script is run.

Spreadsheet = pandas.DataFrame()
Sheetname = 'Makino Machine Metrics ' + time.strftime("%m/%d/%Y") +'.xlsx'
writer = pandas.ExcelWriter(Sheetname, engine = 'xlsxwriter')
Spreadsheet.to_excel(writer, sheet_name= 'Results',index = False)
workbook = writer.book
worksheet = writer.sheets['Results']
writer.save() 

Thanks in Advance for the help

like image 644
LMLPP Avatar asked Dec 11 '25 23:12

LMLPP


1 Answers

When I run your program I get the following error:

IOError: [Errno 2] No such file or directory: 
         'Makino Machine Metrics 07/12/2016.xlsx'

You can fix it by removing the forward slashes from the date part:

import pandas
import time

Spreadsheet = pandas.DataFrame()
Sheetname = 'Makino Machine Metrics ' + time.strftime("%m_%d_%Y") +'.xlsx'
writer = pandas.ExcelWriter(Sheetname, engine = 'xlsxwriter')

Spreadsheet.to_excel(writer, sheet_name='Results', index=False)
workbook = writer.book
worksheet = writer.sheets['Results']
writer.save() 

Looks like the error comes from open:

 >>> open('12/12/12.xlsx', 'w')   
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 IOError: [Errno 2] No such file or directory: '12/12/12.xlsx'

So, either escape those characters or use another character in the date, like in the example above.

like image 167
jmcnamara Avatar answered Dec 13 '25 12:12

jmcnamara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!