Home » excel » python – Saving a Excel file with current date in python3

python – Saving a Excel file with current date in python3

Posted by: admin April 23, 2020 Leave a comment

Questions:

I would like to save the excel file(File Name- Stock) in python along with current date, let me know how to save it with date.
Below is the Script am using normally to save the excel files.

 path=r"\C:\User\ASL - Stock Reports\Stock.xlsx"
 writer=pd.ExcelWriter(path,engine='xlsxwriter')
 Overall_Stock.to_excel(writer, index=False)
 writer.save()
 writer.close()

Thanks in advance

How to&Answers:

Something like this:

from datetime import datetime

curr_date = datetime.strftime(datetime.now(), '%Y_%m_%d')

Overall_Stock.to_excel(path.split('.xlsx')[0] + '_' + curr_date + '.xlsx',  index=False)

Answer:

import datetime
import pandas as pd
now = datetime.datetime.now()
date = '{}-{}-{}'.format(now.year, now.month, now.day)
filename = 'Name_of_your_file' + '_' + date


path=r"\C:\User\ASL - Stock Reports\Stock.xlsx"
writer=pd.ExcelWriter(path, sheet_name = filename, engine='xlsxwriter')
Overall_Stock.to_excel(writer, index=False)
writer.save()
writer.close()

This should work. Let me know if it does not.