In this post we will be creating a Python script that will generate an EAN-8 barcode using the ReportLab library. ReportLab is a powerful Python library that lets you create professional-looking PDF documents. It also provides support for generating various types of barcodes. With these barcode types, you can easily create custom barcodes to make your documents stand out and automate your business processes.
EAN-8 barcodes are 8 digits long and are similar to the EAN-13 barcodes. Companies use EAN-13 barcodes to encode a 13-digit product identifier, but they choose the EAN-8 because it is shorter and more compact. One would typically use EAN-8 for smaller products or items that do not have enough space to accommodate a larger barcode.
See the sample of Python code below where we use the ReportLab library to generate an EAN-8 barcode. We start by creating a new PDF document called ‘barcode.pdf’ and assign it to the variable ‘myCanvas’. We then create a new instance of the
Ean8BarcodeWidget class and pass our eight-digit product identifier “56985631” as an argument. A new instance of the
Drawing class is then created which will hold the barcode widget. We then add the barcode widget to the drawing object and draw the barcode on the PDF document. The PDF document is then saved and the canvas is closed.
from reportlab.graphics.barcode import eanbc from reportlab.pdfgen import canvas from reportlab.graphics.shapes import Drawing myCanvas = canvas.Canvas("barcode.pdf") newBarcode = eanbc.Ean8BarcodeWidget("56985631") barcode_drawing = Drawing() barcode_drawing.add(newBarcode) barcode_drawing.drawOn(myCanvas, 50,50) myCanvas.save()
An image of the barcode in our PDF can be seen below.
Take a look at some of our other content around the Python programming language by clicking here.