如何读取 Excel 文件,创建二维码?

我正在尝试使用此代码


from openpyxl import load_workbook

import qrcode


wb = load_workbook("D:\QR\qrcodes.xlsx")  

ws = wb.['Sheet1']  

column = ws['A']  # Column

data = [column[x].value for x in range(len(column))]


print(data)

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


ext=".png"

for images in data:

    qr.add_data(i)

    qr.make(fit=True)

    img=qr.make_image()

    img.save("{}{}".format(i,ext))

但是在每个循环之后,创建的图像也包含前一个图像的值,如何解决这个问题?


qq_笑_17
浏览 183回答 3
3回答

猛跑小猪

您正在循环外创建 QR 对象。您最好在 for 循环中初始化对象<class 'qrcode.main.QRCode'>并使用函数来创建 QR 图像,因为函数内的变量具有本地范围。ext=".png"def createQr(data):&nbsp; &nbsp; qr = qrcode.QRCode(version = 1, error_correction = qrcode.constants.ERROR_CORRECT_H,box_size = 10, border = 4)&nbsp; &nbsp; qr.add_data(data)&nbsp; &nbsp; qr.make(fit=True)&nbsp; &nbsp; img=qr.make_image()&nbsp; &nbsp; return imgfor i in data:&nbsp; &nbsp; img = createQr(i)&nbsp; &nbsp; img.save("{}{}".format(i,ext))同样正如@martineau所提到的,您必须将循环变量从图像更改为i

12345678_0001

从 Excel 文件中,它从第二行开始读取“A”列中的数据并生成其 QR 码,并创建一个名为“qrcode_produced”的新 Excel 文件,该文件在 B 列中生成 QR 码。# modules neededimport qrcodefrom tkinter import filedialogfrom tkinter import *import openpyxlfrom openpyxl import Workbookfrom openpyxl.styles import Alignmentfrom openpyxl import load_workbook#select the excel file to be read# the texts must be in the "A" column starting with "2" row. In the B column, qrcodes will be seen.print('select xlsx file:')root = Tk()root.filename =&nbsp; filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("xlsx files","*.xlsx"),("all files","*.*")))print (root.filename)# select the folder to save qrcodes as png format images and excel file with qrcodesprint('where to save excel file and qrcodes:')root2 = Tk()root2.withdraw()folder_selected = filedialog.askdirectory()# read the excel fileworkbook = load_workbook(str(root.filename))sheet = workbook.active# settings for qrcode to be producedqr = qrcode.QRCode(&nbsp; &nbsp; version=1,&nbsp; &nbsp; error_correction=qrcode.constants.ERROR_CORRECT_L,&nbsp; &nbsp; box_size=4,&nbsp; &nbsp; border=2,)# excel file cell size settings that will be produced&nbsp;sheet.column_dimensions['B'].width = 25for i in range(1,len(sheet['A'])+1):&nbsp; &nbsp; sheet.row_dimensions[i+1].height=150# Title of B columnsheet["B1"]="Qr_Codes"# production of qrcodes for each row in the A column except first row. Skips the empty rows.for i in range(2,len(sheet['A'])+1):&nbsp; if sheet.cell(row=i, column=1).value is None:&nbsp; &nbsp; continue&nbsp;&nbsp; else:&nbsp; &nbsp; qr.add_data(str(sheet.cell(row=i, column=1).value))&nbsp; &nbsp; qr.make(fit=True)&nbsp; &nbsp; img = qr.make_image()&nbsp; &nbsp; img.save(folder_selected + "/" + "row_"+str(i)+"_qrcode.png")&nbsp; &nbsp; img=openpyxl.drawing.image.Image(folder_selected + "/" + "row_"+str(i)+"_qrcode.png")&nbsp; &nbsp; img.anchor = "B" + str(i)&nbsp; &nbsp; sheet.add_image(img)&nbsp; &nbsp; sheet["B" + str(i)].alignment = Alignment(horizontal='center', vertical='center')&nbsp; &nbsp; sheet["A" + str(i)].alignment = Alignment(horizontal='center', vertical='center')# saving the excel fileworkbook.save(folder_selected+ "/qrcode_produced.xlsx")

拉丁的传说

首先请转换为csv。然后你应该添加枚举,这样你的文件名也会有一个索引号,而不是有多个 file.jpg。import csvimport qrcodewith open('D:\QR\qrcodes.csv') as csvfile:&nbsp; &nbsp; fieldnames= ["Your_Column"]&nbsp; &nbsp; reader= csv.reader(csvfile)&nbsp; &nbsp; qr = qrcode.QRCode(&nbsp; &nbsp; version=1,&nbsp; &nbsp; error_correction=qrcode.constants.ERROR_CORRECT_L,&nbsp; &nbsp; box_size=10,&nbsp; &nbsp; border=4,&nbsp; &nbsp; )&nbsp; &nbsp; for i, row in enumerate(reader):&nbsp; &nbsp; &nbsp; &nbsp; labeldata = row[0]&nbsp; &nbsp; &nbsp; &nbsp; qr.add_data(labeldata)&nbsp; &nbsp; &nbsp; &nbsp; qr.make(fit=True)&nbsp; &nbsp; &nbsp; &nbsp; img = qr.make_image()&nbsp; &nbsp; &nbsp; &nbsp; img.save("test{}.jpg".format(i))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python