简体   繁体   English

在 python3 中保存具有当前日期的 Excel 文件

[英]Saving a Excel file with current date in python3

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.我想将 excel 文件(文件名 - Stock)与当前日期一起保存在 python 中,让我知道如何用日期保存它。 Below is the Script am using normally to save the excel files.下面是我通常用来保存 excel 文件的脚本。

 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提前致谢

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.如果没有,请告诉我。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM