一. dict 转 excel
import pandas as pd
def dict2Xls(data, rule):
downFile = 'static/xls/'+datetime.datetime.now().strftime("H%M%S")+'.xlsx'
file_path = pd.ExcelWriter(downFile)
orders = list(rule.keys())
pf = pd.DataFrame(data)
pf = pf[orders]
pf.rename(columns = rule, inplace = True)
pf.to_excel(file_path, sheet_name='sheet1', encoding = 'utf-8',index = False)
file_path.save()
file_path.close()
二. excel 转 dict
import pandas as pd
def xls2Dict(path_file, rule):
df = pd.read_excel(path_file, sheet_name="Sheet1")
df = df.rename(columns = rule)
data_dict = df.to_dict(orient='records')
return data_dict