简体   繁体   中英

will not generate barcode

I was trying to create a bar code generator in python using a fairly simple code. I am new to some this module and am a bit rusty to python so I was wondering what causes an error in the code

#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

You can use the ImageWriter to save as png files. Import and use when creating the barcode.

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}')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM