无法解码二维码文字

问题描述 投票:0回答:1

我正在使用 pyzbar 解码二维码并使用 Xlwings 将数据保存到 Excel,但是解码失败

import xlwings as xw
import os
import cv2
import numpy as np
from pyzbar.pyzbar import decode
from datetime import datetime
import xlwings as xw
import glob
import time

def main():
     png_files = '1.png'
        
     wb = xw.Book('2.xlsm')  # this will open a new workbook
     sht = wb.sheets[0]
     a_column = wb.sheets[0].range('A:C')# 選取 A 欄
     a_column.clear_contents()# 清空 A 欄的內容
     
     data_list=[]
     filelist=[]
     timelist=[]

     sht.range('A1').value ='圖片名稱'
     sht.range('B1').value ='掃描時間'
     sht.range('C1').value ='條碼內容'

     img=cv2.imread(png_files)
     qrcode=decode(img)
     qr_text=qrcode.data.decode('utf-8')
     data_list.append(qr_text)
     filelist.append(png_files)
     timelist.append(str(datetime.now().strftime('%Y%m%d%H%M%S')))

     c = list(zip(filelist,timelist, data_list))
     fr='A2:C'+str((len(c)+2))
     sht.range(fr).value =c

下一步去掉,用简单的代码,还是解码不了

qrcode=decode(img) # stop here
excel qr-code xlwings decoder
1个回答
0
投票

我编辑了这个问题,因为这个问题实际上是关于 QRCode 的解码。
我猜你在运行该行时遇到了列表属性错误

qr_text=qrcode.data.decode('utf-8')

但是,由于您没有指定实际错误是什么,我没有在编辑中包含它,以防它不正确。
You should update the question and clarify the error,文本和代码中的位置。

解决问题
当你执行这条线时

qrcode=decode(img)

qrcode 将是一个包含 1 个或多个元素的列表,因此 qrcode 没有属性“数据”。您必须先选择元素,然后才能访问数据。因此,对于第一个元素,您应该将行从

qr_text=qrcode.data.decode('utf-8')

qr_text = qrcode[0].data.decode('utf-8')   

或者您可以使用 for 循环遍历元素

for item in qrcode:
    print(item.data.decode("utf-8")) 
© www.soinside.com 2019 - 2024. All rights reserved.