Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Saving an Excel file with the current date

I'm trying to save a new Excel File in the same folder with the original file name and current date, using Python 3.6 and the Datetime module.

The file is saved, but when I open the created file it says Excel cannot open the file 'AllData 2017_08_04.xlsm' because the file format or file extension is not valid.

How can I get the file to save correctly with the datestring included?

from datetime import datetime

os.chdir(r'\\URL\All Data Folder')
wb = openpyxl.load_workbook('AllData.xlsm')
datestring = datetime.strftime(datetime.now(), ' %Y_%m_%d')
wb.save('AllData' + datestring + '.xlsm')
like image 628
Iwan Avatar asked Aug 12 '26 20:08

Iwan


1 Answers

This should work for your issue. Use pandas instead

import pandas as pd
from datetime import datetime

#Use pandas to adress the issue

os.chdir(r'\\URL\All Data Folder')
wb=pd.read_excel(io=r"YOUR PATH\AllData.xlsm")#Fill in your path
datestring = datetime.strftime(datetime.now(), ' %Y_%m_%d')

wb.to_excel(excel_writer=r"YOUR PATH\{0}".format('AllData' + datestring + '.xlsm'))#Fill in your path

If this does not work, look at this Question which should answer your problem because it is about how to save a excel file with xlsm ending

like image 157
2Obe Avatar answered Aug 15 '26 10:08

2Obe



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!