網站首頁 編程語言 正文
前言
今天教大家利用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('寫入失敗')
原文鏈接:https://blog.csdn.net/weixin_69999177/article/details/125417757
相關推薦
- 2022-10-24 Golang?errgroup?設計及實現原理解析_Golang
- 2022-05-13 python list.sort()方法排序一探究竟
- 2022-07-07 Python數據分析之?Matplotlib?折線圖繪制_python
- 2022-06-11 Python語法學習之進程池與進程鎖詳解_python
- 2022-10-05 Android模仿Toast實現提示框效果_Android
- 2022-06-28 Python利用shutil模塊實現文件的裁剪與壓縮_python
- 2022-07-01 tensorflow可視化Keras框架中Tensorboard使用示例_python
- 2022-05-16 C#?CM框架實現多頁面管理的實例代碼_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支