Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a variable to generate barcode with python-barcode

Tags:

python

barcode

I'm trying to generate a barcode using python-barcode (https://github.com/WhyNotHugo/python-barcode). I manage to get a barcode generated and saved as png like this:

import barcode
from barcode.writer import ImageWriter

EAN = barcode.get_barcode_class('ean13')
ean = EAN(u'5901234123457', writer=ImageWriter())
fullname = ean.save('barcode')

However, if I try to use the value from a variable, it doesn't work:

import barcode
from barcode.writer import ImageWriter

number = 5901234123457    

EAN = barcode.get_barcode_class('ean13')
ean = EAN(number, writer=ImageWriter())
fullname = ean.save('barcode')

I then get

TypeError: 'int' object is not subscriptable

I might be making some silly mistake, but I'm quite new to this... :/

like image 499
fillefrans Avatar asked Jun 01 '26 12:06

fillefrans


1 Answers

the barcode module only accepts strings as input, you should then just make that integer a string:

import barcode
from barcode.writer import ImageWriter

number = 5901234123457 
number = str(number)

EAN = barcode.get_barcode_class('ean13')
ean = EAN(number, writer=ImageWriter())
fullname = ean.save('barcode')
like image 86
Liam Avatar answered Jun 04 '26 00:06

Liam