• 利用Python制作本地Excel的查询与生成的程序


    目录

    前言

    需求

    实验步骤

    Excel预览图片

    查询

    追加查询结果到Excel

    完整代码

    前言

    大家好 我是毕加锁(锁!)

    今天教大家利用Python制作本地Excel的查询与生成的程序

    需求

    制作一个程序 有一个简单的查询入口 实现Excel的查询与生成

    实验步骤

    1打开一个exe 弹出一个界面

     2有一个查询 卡号 点击查询

    3下方展示查询的结果 同时将这个查询的结果 追加到一个新的结果Excel文件里

    4新的结果Excel文件 格式和源文件格式相同 但是每次都在最后追加

    今天教大家利用Python制作本地Excel的查询与生成的程序

    Excel预览图片

    1.2 导入模块并读取Excel文件

    等会要用的模块有:pandas、os、xlwt和uuid
    用import导入的代码:

    import pandas, os, xlwt, uuid
    

    导入好后,就要读取Excel文件了。读取Excel要用到pandas的read_excel函数。

    1. try:
    2.     exl = pandas.read_excel(aim_path)
    3. except:
    4.     print('找不到文件!请检查一下文件路径或文件是否存在')
    5.     os._exit(0)

    刚刚导入os模块就是为了做异常捕获找不到文件时的退出。

    查询

    2.1 Excel的索引与输入

    为了方便后面查询,要把DataFrame的索引(index)设为查询输入的卡号。接着,输出以卡号为索引的DF,以便用户查询。最后,就开始循环输入了。

    1. exl.set_index('卡号', inplace = True)
    2. print(f'{exl}\n')
    3. while 1:
    4.     try:
    5.         idx = input('卡号(输入“退出”即可退出):')
    6.         if idx == '退出':
    7.             os._exit(0)

    2.2 开始查询、丰富程序

    查询用dataframe.loc[index]来完成,最后输出返回的Series。为了避免用户输入非卡号信息,就又加了异常捕获。

    1.         res = exl.loc[idx]
    2.         print(f'\n{res}\n')
    3.     except KeyError:
    4.         print('你的卡号可能输错了!我找不到这个卡号的人哦~\n')
    5.         continue
    6.     except:
    7.         print('有些错误发生了!\n')
    8.         continue

    追加查询结果到Excel

    3.1 读取或新建Excel

    3.1.1 读取

    读取跟上面一样,用read_excel

    1.     try:
    2.         res_exl = pandas.read_excel(res_path)

    3.1.2 新建Workbook和Sheet

    现在轮到xlwt模块大展身手啦~ 用Workbook函数来新建Workbook;用add_sheet函数新增Sheet

    1.     except:
    2.         workbook = xlwt.Workbook()
    3.         sheet = workbook.add_sheet('new')
    4.         col = 0

    3.1.2 写入Column

    在Column的位置,需要填入查询的Excel的列索引,用

    1. list(pandas.read_excel(aim_path).columns.values)

    可以获取到。然后把列索引以xlwt.write填进去,最后把DF保存再读取这个Excel。

    1. for i in list(pandas.read_excel(aim_path).columns.values):
    2.             sheet.write(0, col, i)
    3.             col += 1
    4.         workbook.save(res_path)
    5.         res_exl = pandas.read_excel(res_path)

    3.2 追加结果

    首先,把结果res变量设置成列表类型。然后,在这个列表里面新增结果没有的卡号。最后把这个列表设置成一个Series(索引为查询的Excel的列索引)。

    1.     res_series_data = list(res)
    2.     res_series_data.insert(2, idx)
    3.     res_series = pandas.Series(
    4.         res_series_data, 
    5.         index = list(
    6.             pandas.read_excel(aim_path).columns.values
    7.         )
    8.     )

    现在建好了Series,准备追加了。追加完后还要保存这个Excel。

    1.     res_exl.loc[str(uuid.uuid1())] = res_series
    2.     try:
    3.         res_exl.to_excel(res_path, index = False)
    4.     except:
    5.         print('写入失败')

    这里用了uuid.uuid1来随机产生索引,避免重复而修改其它人的值。最后几行就是保存的操作,python index = False的意思就是把索引隐藏掉了。

    完整代码

    1. try:
    2.     exl = pandas.read_excel(aim_path)
    3. except:
    4.     print('找不到文件!请检查一下文件路径或文件是否存在')
    5.     os._exit(0)
    6. exl.set_index('卡号', inplace = True)
    7. print(f'{exl}\n')
    8. while 1:
    9.     try:
    10.         idx = input('卡号(输入“退出”即可退出):')
    11.         if idx == '退出':
    12.             os._exit(0)
    13.         res = exl.loc[idx]
    14.         print(f'\n{res}\n')
    15.     except KeyError:
    16.         print('你的卡号可能输错了!我找不到这个卡号的人哦~\n')
    17.         continue
    18.     except:
    19.         print('有些错误发生了!\n')
    20.         continue
    21.     try:
    22.         res_exl = pandas.read_excel(res_path)
    23.     except:
    24.         workbook = xlwt.Workbook()
    25.         sheet = workbook.add_sheet('new')
    26.         col = 0
    27.         for i in list(pandas.read_excel(aim_path).columns.values):
    28.             sheet.write(0, col, i)
    29.             col += 1
    30.         workbook.save(res_path)
    31.         res_exl = pandas.read_excel(res_path)
    32.     res_series_data = list(res)
    33.     res_series_data.insert(2, idx)
    34.     res_series = pandas.Series(
    35.         res_series_data, 
    36.         index = list(
    37.             pandas.read_excel(aim_path).columns.values
    38.         )
    39.     )
    40.     res_exl.loc[str(uuid.uuid1())] = res_series
    41.     try:
    42.         res_exl.to_excel(res_path, index = False)
    43.     except:
    44.         print('写入失败')

    我是毕加锁 (锁!) 期待你的关注

  • 相关阅读:
    Day42 尚硅谷JUC——Fork_Join分支合并框架
    tf.nn
    计算机网络【UDP与TCP协议(三次握手、四次挥手)】
    基于html+css+js的图书管理系统
    《统计学习方法》 第二十一章 PageRank算法
    TCP 和UDP通信流程
    【Android -- 开发】初级工程师进阶
    Fiber 架构实现流程
    FITC-PEG-FA,Folic acid-PEG-Fluorescein,叶酸PEG荧光素
    【EI会议征稿】第八届能源系统、电气与电力国际学术会议(ESEP 2023)
  • 原文地址:https://blog.csdn.net/weixin_69999177/article/details/125417757