日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

利用Python制作本地Excel的查詢與生成的程序問題_python

作者:畢加鎖 ? 更新時間: 2022-08-21 編程語言

前言

今天教大家利用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

欄目分類
最近更新