Member-only story
Barcode using Python
2 min readAug 26, 2024
pip install python-barcode
import barcode
from barcode.writer import ImageWriter
from IPython.display import Image, display
barcode_format = barcode.get_barcode_class('ean13')
barcode_number = '123456789012'
barcode_image = barcode_format(barcode_number, writer=ImageWriter())
barcode_filename = 'barcode_image'
barcode_image.save(barcode_filename)
display(Image(filename=f'{barcode_filename}.png'))
Here’s a step-by-step explanation of the code:
1. Importing Required Libraries
import barcode
from barcode.writer import ImageWriter
from IPython.display import Image, display
import barcode
: Imports thebarcode
module, which provides the tools needed to generate various types of barcodes.from barcode.writer import ImageWriter
: Imports theImageWriter
class from thebarcode.writer
module. This class is used to create barcode images (e.g., PNG files) instead of just the barcode as text.from IPython.display import Image, display
: ImportsImage
anddisplay
fromIPython.display
. These are used to display the generated barcode image directly within a Jupyter Notebook.
2. Selecting the Barcode Format
barcode_format = barcode.get_barcode_class('ean13')