pandas自动将excel归类到按日期生成的文件夹中
通过pandas对原excel的处理后
import pandas as pd
import os
import datetime
df=pd.read_csv(r'xxxxxxx.csv',encoding='gbk')
# print(df)
df['店仓名称']=df['店仓名称'].str[2:-1]
summary=df.groupby('店仓名称').sum()[['采购入库数量','采购入库金额']]
path=r'G:\EricWorking\采购入库报表'
#根据日期生成新的月度文件夹地址
year_month=str(datetime.datetime.now().year)+str(0)+str(datetime.datetime.now().month)
path=os.path.join(path,year_month)
print(path)
#在一个workbook里面加sheet
if os.path.exists(path):
print(1)
save_path=os.path.join(path,f'采购入库报表{datetime.datetime.now().strftime("%Y%m%d")}.xlsx')
with pd.ExcelWriter(save_path) as writer:
df.to_excel(writer,index=None)
pd.DataFrame(summary).to_excel(writer,sheet_name='总计')
else:
print(2)
os.makedirs(path)
save_path=os.path.join(path,f'采购入库报表{datetime.datetime.now().strftime("%Y%m%d")}.xlsx')
with pd.ExcelWriter(save_path) as writer:
df.to_excel(writer,index=None)
pd.DataFrame(summary).to_excel(writer,sheet_name='总计')