python批量生成二维码

python批量生成二维码


安装相关库(注意要按顺序安装)

pip install pillow
pip install qrcode

代码:

import qrcode
import xlrd


def getInfo():
    try:
        data = xlrd.open_workbook(r"C:\Users\USER\Desktop\BIAO.xlsx")
        sheetname = "Sheet1"
        table = data.sheet_by_name(sheetname)
        col_values = table.col_values(0)
        return col_values
    except Exception as e:
        print(e)

def getImage():
    machList = getInfo()
    s = 0
    for machCode in machList:
         #   要放在循环里,否则 qr.add_data(filename) 会一直累加
        qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=1)        
        filename = "AAA-MA:" + machCode
        qr.add_data(filename)
        qr.make(fit=True)
        img = qr.make_image()
        file = "image/{0}.png".format(machCode)
        img.save(file)
        s += 1

getImage()

你可能感兴趣的:(python)