xlwings 二维码在 excel 中解码故障

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

似乎不能使用 decode() '------------ 将 xlwings 导入为 xw 导入操作系统 导入cv2 将 numpy 导入为 np 从 pyzbar.pyzbar 导入解码 从日期时间导入日期时间 将 xlwings 导入为 xw 导入 glob 导入时间

定义主要(): 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) # 到此为止

excel xlwings
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.