Python ReportLab – Generate a Code 39 barcode

In this post we will be creating a Python script that will generate a Code 39 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.

Industries commonly use the Code 39 barcode type for tracking and inventory management in logistics, manufacturing, and other areas. This type of barcode is linear and so bars and spaces run parallel to the length of the barcode.

See the sample of Python code below where we use the ReportLab library to generate a Code 39 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 Code 39 class named ‘newBarcode’ and pass the string “235698457” as an argument to generate the barcode. Our ‘newBarcode’ object is then drawn onto the PDF canvas at the position (50, 50) on the page. The PDF document is then saved and the canvas is closed.

from reportlab.graphics.barcode import code39
from reportlab.pdfgen import canvas

myCanvas = canvas.Canvas("barcode.pdf")

newBarcode = code39.Standard39("235698457")

newBarcode.drawOn(myCanvas, 50, 50)

myCanvas.save()

An image of the barcode in our PDF can be seen below.

Python ReportLab - Generate a Code 39 barcode

Take a look at some of our other content around the Python programming language by clicking here.

Leave a Reply