簡體   English   中英

不會生成條形碼

[英]will not generate barcode

我試圖使用相當簡單的代碼在 python 中創建一個條形碼生成器。 我是這個模塊的新手,對 python 有點生疏,所以我想知道是什么導致了代碼錯誤

#imports the correct module needed to create the barcodes 
from barcode import EAN13
# imports a module that will create the numbers needed for the barcode
from random import randint

codes = []
barcodes = []
for i in range(100):
    code = randint(1111111111111, 9999999999999)
    while code not in codes:
        codes.append(code)

# creates the barcode object   
for code in codes: 
    barcode = EAN13("{codes}")
    barcodes.append(barcode)


# prints the barcode
print(barcodes(0))



Error caused
will not generate any barcodes because the number cant be saved as a string

i want this to be able to display barcodes

您可以使用 ImageWriter 將其另存為 png 文件。 創建條形碼時導入和使用。

from barcode import EAN13
from random import randint
from barcode.writer import ImageWriter

codes = []
barcodes = []
for i in range(100):
    code = randint(1111111111111, 9999999999999)
    while code not in codes:
        codes.append(code)

# creates the barcode object
for code in codes:
    code = str(code)
    # barcode = EAN13(code)
    barcode = EAN13(code, writer=ImageWriter())
    barcodes.append(barcode)

# render the first barcode in the list
# barcodes[0].render()

for bc in barcodes:
    bc.save(f'barcode{bc.ean}')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM