Python制作本地Excel查看与形成程序流程
本际云服务器推荐网的小编小本本今天教给大家如何运用Python制作本地Excel的查看与形成程序流程。

要求
创建一个程序流程有简单的查询入口完成Excel的查看与形成。
实验步骤
1. 开启一个exe弹出来一种页面。
2. 有个查询卡号查询按钮。
3. 下边展现查询结论与此同时把这个查询结论增加到一个全新的结论Excel文件里。新得到的结果Excel文件文件类型和源文件类型同样可是每次都是在最终增加。
Code
导入控制模块并获取Excel文件。要用到的控制模块有:pandas、os、xlwt和uuid。用import导进的编码:
代码预览
import pandas,os,xlwt,uuid
try:
exl=pandas.read_excel(aim_path)
except:
print('找不到文件!请检查一下文件路径或文件是否存在')
os._exit(0)
Excel的索引与输入。为了方便后面查询,要把DataFrame的索引(index)设为查询输入的卡号。接着,输出以卡号为索引的DF,以便用户查询。最后,就开始循环输入。
代码预览
exl.set_index('卡号',inplace=True)
print(f'{exl}n')
while 1:
try:
idx=input('卡号(输入“退出”即可退出):')
if idx=='退出':
os._exit(0)
res=exl.loc[idx]
print(f'n{res}n')
except KeyError:
print('你的卡号可能输错了!我找不到这个卡号的人哦~n')
continue
except:
print('有些错误发生了!n')
continue
追加查询结果到Excel。读取或新建Excel文件类型和源文件类型同样可是每次都是在最终增加。
代码预览
try:
res_exl=pandas.read_excel(res_path)
except:
workbook=xlwt.Workbook()
sheet=workbook.add_sheet('new')
col=0
for i in list(pandas.read_excel(aim_path).columns.values):
sheet.write(0,col,i)
col+=1
workbook.save(res_path)
res_exl=pandas.read_excel(res_path)
res_series_data=list(res)
res_series_data.insert(2,idx)
res_series=pandas.Series(
res_series_data,
index=list(
pandas.read_excel(aim_path).columns.values
)
)
res_exl.loc[str(uuid.uuid1())]=res_series
try:
res_exl.to_excel(res_path,index=False)
except:
print('写入失败')
结尾
以上就是本际云服务器推荐网小编小本本教给大家如何运用Python制作本地Excel的查看与形成程序流程。希望能够对大家有所帮助!
原创文章,作者:小编小本本,如若转载,请注明出处:https://www.benjiyun.com/yunzhujiyunwei/vps-yunwei/6864.html
