Thursday, 12 December 2013

Generating Barcodes In Python

There are almost 2.7 million searches related to the term “barcode” each and every month on Google.
 It’s no wonder that barcoding is such a large topic for small businesses. Can you think of anything in this day and age that doesn’t have a barcode?
Barcodes have revolutionized the way we do business today. They allow you to run your small business more accurately and efficiently, getting you ready for growth. You’ll look more professional too!
Programmers these days are required to know just enough about barcodes in order to implement any serious shop management system or product identification system. However the barcode specification spans many pages. This means, it's impractical for any programmer to fully understand how barcodes work behind the scenes before being able to generate them from their codes.

There are a lot of software libraries out there to help in the generation and scanning of barcodes. For example, the ZXing library is a suite of java libraries for working with barcodes from the Android platform to JSE and JEE (Servlet Containers).
There is also Barcode4J, a freely available software library for generating and scanning barcodes in java.

Scanning of barcodes in a shop management system is actually a quick replacement for the keyboard. That is, instead of searching products by typing lengthy product codes into the software, a barcode scanner simply scans the barcode image and feeds the product code into the software for searching.

Of all the libraries around, I choose ReportLab, an amazingly free python package for generating PDFs. Aside it's extensive support for PDFs, it generates all kinds of barcodes including QRCodes.

The procedure for installing ReportLab is clearly laid out on their site.

Now with ReportLab installed, let's get our fingers dirty with some python codes
In our first example we will generate a simple PDF document showing "Hello World"
Open up your text editor and type in the following codes

from reportlab.pdfgen import canvas

c = canvas.Canvas("hello.pdf")
c.drawString(100,500,"Hello World")

After executing this python code, you should see a file, "hello.pdf" in your current working directory. Open it with any PDF reader and say a big thanks to the ReportLab team for such a fantastic offer.

The details of how this piece of code works is spelt out in the ReportLab documentation.

Now let's generate some simple barcode. The details of the different types of barcodes are out of this post's scope but in this example we will be generating a EAN13 barcode, an european standard barcode format for most products in shop. The ReportLab library is capable of generating all other kinds of barcodes. Just read the fu*king manual.

from import eanbc
from import Drawing
from reportlab.pdfgen import canvas
from import renderPDF

#Setting up canvas for pdf
c = canvas.Canvas("barcode.pdf")
code_number = "1234567899087" #This is the number for which the barcode is to be generated

#Generating a EAN13 code
barcode = eanbc.Ean13BarcodeWidget(code_number)

#Drawing generated barcode onto pdf
drawing = Drawing(50,10)

This is all there's to generating and saving a barcode to a PDF. Applying your own logic, you should be able to generate countless unique barcodes, print them out and stick them on your products for identification.

Lemme know of your questions by commenting.

Happy Coding!!!