目录
  • 前言
  • 需求
  • 实验步骤
  • Excel预览图片
  • 查询
    • 2.1 Excel的索引与输入
    • 2.2 开始查询、丰富程序
  • 追加查询结果到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函数。

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

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

      查询

      2.1 Excel的索引与输入

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

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

      2.2 开始查询、丰富程序

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

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

      追加查询结果到Excel

      3.1 读取或新建Excel

      3.1.1 读取

      读取跟上面一样,用read_excel

          try:
              res_exl = pandas.read_excel(res_path)

      3.1.2 新建Workbook和Sheet

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

          except:
              workbook = xlwt.Workbook()
              sheet = workbook.add_sheet('new')
              col = 0

      3.1.2 写入Column

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

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

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

      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)

      3.2 追加结果

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

          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
              )
          )

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

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

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

      完整代码

      try:
          exl = pandas.read_excel(aim_path)
      except:
          print('找不到文件!请检查一下文件路径或文件是否存在')
          os._exit(0)
      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
       
          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('写入失败')
      声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。