Generate barcode in Python using python-barcode

This Python tutorial to show you how to generate different type of barcode images in Python programs using the using python-barcode package. With different Python example program we learn to generate popular barcode types such as Code 39, Code 128, PZN, EAN-13, EAN-8, JAN, ISBN-13, ISBN-10, ISSN, UPC-A.

Table of contents

  1. Install python-barcode package
  2. Generate Code 39 barcode
  3. Generate Code 128 barcode
  4. Generate PZN barcode
  5. Generate EAN-13 barcode
  6. Generate EAN-8 barcode
  7. Generate JAN barcode
  8. Generate ISBN-13 barcode
  9. Generate ISBN-10 barcode
  10. Generate ISSN barcode
  11. Generate UPC-A barcode
  12. Generate barcode as SVG file

Install python-barcode package

Using the following command to install python-barcode package and its additional dependencies for barcode image generating.

pip install "python-barcode[images]"

Generate Code 39 barcode image in Python

The following Python program we generate the Code 39 barcode type image.

import barcode
from barcode.writer import ImageWriter

code = 'TORICODE'

sample_barcode = barcode.get('code39', code, writer=ImageWriter())
generated_filename = sample_barcode.save('barcode1')

print('Generated Code 39 barcode image file name: ' + generated_filename)
The program output.
Generated Code 39 barcode image file name: barcode1.png

Execute the program above it will generate the barcode image named barcode1.png as below.

Barcode

Generate Code 128 barcode image in Python

The following program to show you how to generate Code 128 barcode in Python.

import barcode
from barcode.writer import ImageWriter

code = 'TORICODE'

sample_barcode = barcode.get('code128', code, writer=ImageWriter())
generated_filename = sample_barcode.save('barcode2')

print('Generated Code 128 barcode image file name: ' + generated_filename)
The program output.
Generated Code 128 barcode image file name: barcode2.png

Execute the program above it will generate the barcode image named barcode2.png as below.

Barcode

Generate PZN barcode image in Python

The Python program below we generate Pharma-Zentral-Nummer (PZN) barcode in Python

import barcode
from barcode.writer import ImageWriter

code = '1234562'

sample_barcode = barcode.get('pzn', code, writer=ImageWriter())
generated_filename = sample_barcode.save('barcode3')

print('Generated PZN barcode image file name: ' + generated_filename)
The program output.
Generated PZN barcode image file name: barcode3.png

Execute the program above it will generate the barcode image named barcode3.png as below.

Barcode

Generate EAN-13 barcode image in Python

The following program to generate EAN-13 (International Article Number) barcode in Python.

import barcode
from barcode.writer import ImageWriter

code = '9780201379624'

sample_barcode = barcode.get('ean13', code, writer=ImageWriter())
generated_filename = sample_barcode.save('barcode4')

print('Generated EAN-13 barcode image file name: ' + generated_filename)
The program output.
Generated EAN-13 barcode image file name: barcode4.png

Execute the program above it will generate the barcode image named barcode4.png as below.

Barcode

Generate EAN-8 barcode image in Python

The following program to generate EAN-8 (International Article Number) barcode in Python.

import barcode
from barcode.writer import ImageWriter

code = '96385074'

sample_barcode = barcode.get('ean8', code, writer=ImageWriter())
generated_filename = sample_barcode.save('barcode5')

print('Generated EAN-8 barcode image file name: ' + generated_filename)
The program output.
Generated EAN-8 barcode image file name: barcode5.png

Execute the program above it will generate the barcode image named barcode5.png as below.

Barcode

Generate JAN barcode image in Python

The following Python program we generate JAN (Japanese Article Numbering) barcode in Python.

import barcode
from barcode.writer import ImageWriter

code = '4901234567894'

sample_barcode = barcode.get('jan', code, writer=ImageWriter())
generated_filename = sample_barcode.save('barcode6')

print('Generated JAN barcode image file name: ' + generated_filename)
The program output.
Generated JAN barcode image file name: barcode6.png

Execute the program above it will generate the barcode image named barcode6.png as below.

Barcode

Generate ISBN-13 barcode image in Python

The program below to generate ISBN-13 (International Standard Book Number) barcode in Python.

import barcode
from barcode.writer import ImageWriter

code = '9781234567897'

sample_barcode = barcode.get('isbn13', code, writer=ImageWriter())
generated_filename = sample_barcode.save('barcode7')

print('Generated ISBN-13 barcode image file name: ' + generated_filename)
The program output.
Generated ISBN-13 barcode image file name: barcode7.png

Execute the program above it will generate the barcode image named barcode7.png as below.

Barcode

Generate ISBN-10 barcode image in Python

The program below to generate ISBN-10 (International Standard Book Number) barcode in Python.

import barcode
from barcode.writer import ImageWriter

code = '1234567'

sample_barcode = barcode.get('isbn10', code, writer=ImageWriter())
generated_filename = sample_barcode.save('barcode8')

print('Generated ISBN-10 barcode image file name: ' + generated_filename)
The program output.
Generated ISBN-10 barcode image file name: barcode8.png

Execute the program above it will generate the barcode image named barcode8.png as below.

Barcode

Generate ISSN barcode image in Python

The Python program below to generate ISSN (International Standard Serial Number) barcode in Python.

import barcode
from barcode.writer import ImageWriter

code = '20492'

sample_barcode = barcode.get('issn', code, writer=ImageWriter())
generated_filename = sample_barcode.save('barcode9')

print('Generated ISSN barcode image file name: ' + generated_filename)
The program output.
Generated ISSN barcode image file name: barcode9.png

Execute the program above it will generate the barcode image named barcode9.png as below.

Barcode

Generate UPC-A barcode image in Python

The Python program below to generate UPC-A(Universal Product Code) barcode in Python.

import barcode
from barcode.writer import ImageWriter

code = '725272730706'

sample_barcode = barcode.get('upca', code, writer=ImageWriter())
generated_filename = sample_barcode.save('barcode10')

print('Generated UPC-A barcode image file name: ' + generated_filename)
The program output.
Generated UPC-A barcode image file name: barcode10.png

Execute the program above it will generate the barcode image named barcode10.png as below.

Barcode

Generate barcode as SVG file in Python

We also can generate barcode as SVG file using the python-barcode package as following Python program.

import barcode

code = '725272730706'

sample_barcode = barcode.get('upca', code)
generated_filename = sample_barcode.save('barcode11')

print('Generated UPC-A barcode SVG file name: ' + generated_filename)
The program output.
Generated UPC-A barcode SVG file name: barcode11.svg

Execute the program above it will generate the barcode as SVG file named barcode11.svg.

Happy Coding 😊