Python批量生成二維碼(Pandas+Tkinter)

本文分享自己寫的一個實用性的python小程序,非常簡單,作用是能夠從excel中讀取一列數(shù)據(jù)然后生成對應(yīng)的所有二維碼,就姑且稱之為批量生成二維碼吧!

廢話不多說,直接上代碼

需要安裝的模塊

  • pandas
  • qrcode
  • xlrd
  • tkinter
  • numpy沒有用到,可以刪除

代碼

代碼部分很簡單,也不長,而且做成了GUI式,只需要把庫安裝全了,直接運行就可以。
主要涉及的知識有

  • tkinter控件的使用
  • pandas讀取excel
  • qrcode生成二維碼
  • sys、os獲取目錄,保存文件
import os
import sys
import tkinter as tk
from tkinter import filedialog

import numpy as np
import pandas as pd
import qrcode

'''
批量生成二維碼
'''

qr = qrcode.QRCode(
    error_correction=qrcode.constants.ERROR_CORRECT_H,
    box_size=10,
    border=4
)


class run:
    def __init__(self, window):
        self.window = window
        self.label_path = tk.StringVar()
        self.curr_path = sys.path[0].replace('/', "\\")
        self.label_path.set('當(dāng)前目錄:'+self.curr_path)
        self.sheet_name = tk.StringVar()
        self.data_col = tk.StringVar()
        self.res_dir = tk.StringVar()
        self.label_res = tk.StringVar()
        self.gui()
        self.window.mainloop()

    def choose_file(self):
        file_name = filedialog.askopenfile(title='選擇文件',
                                           filetypes=[("excel文件", "*.xlsx"),
                                                      ('excel2003文件', '*.xls')],
                                           initialdir='g:/')
        self.label_path.set('當(dāng)前選擇:'+file_name.name.replace('/', "\\"))

    def check_dir(self, dir):
        pathdir = self.curr_path+'\\'+dir
        isexit = os.path.exists(pathdir)
        if not isexit:
            os.makedirs(pathdir)
        return pathdir

    def insert_start(self, var):
        self.t4.insert(1.0, var)

    def start(self):
        sheet_name = self.sheet_name.get()
        data_col = self.data_col.get()
        res_dir = self.res_dir.get()
        path_dir = self.check_dir(res_dir)
        labelpath = self.label_path.get()[5::]
        df = pd.read_excel(labelpath, sheet_name=sheet_name)
        file = df.loc[:, [data_col]].values.ravel()
        i = 0
        for readline in file:
            qr.add_data(readline)
            qr.make(fit=True)
            img = qr.make_image()
            filename = str(readline)+'.png'
            readline = ''
            img.save(path_dir+'\\'+filename)
            qr.clear()
            i = i+1
            res = '\ndone No. '+str(i)
            self.insert_start(res)
            print('done No. '+str(i))
            self.window.update()
        res_total = 'sucess '+str(i)+'!'
        self.insert_start(res_total)
        print(res_total)
        self.window.update()

    def gui(self):
        # 路徑label
        self.l0 = tk.Label(self.window, textvariable=self.label_path,
                           fg='black', height=2, wraplength=400,
                           justify='left', font=('Arial', 12))
        self.l0.place(x=20, y=0, anchor='nw')
        # 選擇button
        self.btnChoose = tk.Button(self.window, text='選擇excel文件', font=('Arial', 12),
                                   width=10, height=1, command=self.choose_file)
        self.btnChoose.place(x=480, y=5, anchor='nw')
        # edit
        self.l1 = tk.Label(self.window, text='請輸入Sheet名:',
                           fg='black', height=2, font=('Arial', 12))
        self.l1.place(x=130, y=50, anchor='nw')
        self.e1 = tk.Entry(self.window, textvariable=self.sheet_name,
                           show=None, font=('Arial', 16))
        self.e1.place(x=260, y=60, anchor='nw')
        self.l2 = tk.Label(self.window, text='請輸入數(shù)據(jù)列名:',
                           fg='black', height=2, font=('Arial', 12))
        self.l2.place(x=130, y=100, anchor='nw')
        self.e2 = tk.Entry(self.window, textvariable=self.data_col,
                           show=None, font=('Arial', 16))
        self.e2.place(x=260, y=110, anchor='nw')
        self.l3 = tk.Label(self.window, text='輸出文件夾名稱:',
                           fg='black', height=2, font=('Arial', 12))
        self.l3.place(x=130, y=150, anchor='nw')
        self.e3 = tk.Entry(self.window, textvariable=self.res_dir,
                           show=None, font=('Arial', 16))
        self.e3.place(x=260, y=160, anchor='nw')
        # 開始
        self.btnstart = tk.Button(self.window, text='開始生成', font=('Arial', 18),
                                  width=10, height=1, command=self.start)
        self.btnstart.place(x=240, y=200)

        # 結(jié)果展示
        self.t4 = tk.Text(self.window, width=600)
        self.t4.place(x=0, y=300)


if __name__ == '__main__':
    w = tk.Tk()
    w.title('批量生成二維碼')
    w.geometry('600x600')
    r = run(w)

效果圖

qr1.JPG

界面布局

很簡單,兩個button,一個label用來顯示選擇的excel文件,三個entry用來獲取表單名、數(shù)據(jù)列名、輸出文件夾名,一個text用來顯示運行結(jié)果。

注意事項

1.表單名、數(shù)據(jù)列名稱、輸出文件夾名是必需的;
2.輸出文件夾用來保存生成的二維碼圖片,保存在代碼文件所在路徑下。

源碼地址:https://github.com/SevenJiao/GetMoreQrCode

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容