Back to Tutorial

How to create Barcodes in python

What is a Barcode?

barcode is an optical, machine-readable , representation of data , the data usually describes something about the object that carries the barcode . Traditional barcodes systematically represent data by varying the widths and spacings of parallel lines, and may be referred to as linear or one-dimensional (1D) ,we can quickly test on console (mac, tested on ubuntu , debian).

PyBarcode ships with a little commandline script to generate barcodes without knowing Python. The install script detects your Python version and adds the major version number to the executable script. On Python 2 it is called pybarcode2 and on Python 3 pybarcode3. When installing in a systemwide direction, you can have pyBarcode installed in Python 2 and 3 at the same time without trouble.

Requirements

  • Setuptools/distribute for installation (new in version 0.7beta4)
  • Python 2.6 or above (including Python 3.x)
  • On Python 2.6, 3.0, 3.1: argparse (for the commandline script)
  • Program to open SVG objects (your browser should do it)
  • Optional: PIL to render barcodes as images (PNG, JPG, …)

Installation

Make sure you have setuptools/distribute installed.

Unpack the downloaded file, cd into the pyBarcode directory and run python setup.py install. Or just copy the barcode dir somewhere in your PYTHONPATH.

The best way is to use pip:

pip install pyBarcode

Provided Barcodes

EAN-8, EAN-13, UPC-A, JAN, ISBN-10, ISBN-13, ISSN, Code 39, PZN

 

Usage form command line

pybarcode{2,3} create “My Text” barcodename

If you know which python version your system have then run accordingly.I am assuming python3.x

pybarcode{2,3} create -t png "My Text" outfile

 

pybarcode3 create "My Text" outfile

or

Sample example to generate QR code and save in svg file in python.

 

ean13 is EAN or European Article Number and 13 representing it is 13 digit number

>>> import barcode
>>> ean = barcode.get('ean13', '123456789102')
# Now we look if the checksum was added
>>> ean.get_fullcode()
u'1234567891026'
>>> filename = ean.save('ean13')
>>> filename
u'ean13.svg'
>>> options = dict(compress=True)
>>> filename = ean.save('ean13', options)
>>> filename
u'ean13.svgz'

Now we have ean13.svg and the compressed ean13.svgz in our current working directory. Open it and see the result.

 

To generate barcodes as images, you must provide the ImageWriter to the get function. Without any options, the images are rendered as PNG.

>>> import barcode
>>> from barcode.writer import ImageWriter
>>> ean = barcode.get('ean13', '123456789102', writer=ImageWriter())
>>> filename = ean.save('ean13')
>>> filename
u'ean13.png'

 

source : Pythonhosted.org

join our python training in kanpur for Zero to Hero in Python.

Share this post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Tutorial