Create Barcodes with Pure Python - Raspberry Pi Projects, Tutorials, Learning DIY Electronics - Makergenix

Breaking

 


Create Barcodes with Pure Python

In this blog we'll learn to creating barcode using python, here we'll be learning creating barcode in 3 different ways:

  • Creating barcodes as SVG
  • Creating barcodes as Image 
  • Create barcodes from the commandline
Create Barcodes with Pure Python

 Note: Pillow is necessary for image output (png, jpg), but not for SVG export.

To make a barcode, you only need to know the system (EAN, UPC, etc.) and the code (e.g. for EAN-13: 123456789102). As you can see, the checksum is calculated automatically, thus you don't need it. The checksum is optional on some systems (Code 39), in which case you can use the add checksum keyword parameter (default is True).

Barcode HAT for Raspberry Pi Pico

Rapsberry Pi Pico Barcode & QR Code Scanner HAT & Breakout - Launched

Creating barcodes as SVG

You may use the default writer to create barcodes as SVG objects (simply not specify a writer).
>>> import barcode
>>> ean = barcode.get('ean13', '123456789102')
# Now we look if the checksum was added
>>> ean.get_fullcode()
'1234567891026'
>>> filename = ean.save('ean13')
>>> filename
'ean13.svg'
>>> options = dict(compress=True)
>>> filename = ean.save('ean13', options)
>>> filename
'ean13.svgz'
In your current working directory, you should have ean13.svg and ean13.svgz. See what happens when you open it.

Creating barcodes as Image

You must pass the ImageWriter to the get method to produce barcodes as images. The images are produced as PNG without any options.
>>> import barcode
>>> from barcode.writer import ImageWriter
>>> ean = barcode.get('ean13', '123456789102', writer=ImageWriter())
>>> filename = ean.save('ean13')
>>> filename
'ean13.png'

Create barcodes from the commandline

python-barcode includes a command-line tool that allows you to produce barcodes without understanding Python. The install script identifies your Python version and updates the executable script with the major version number.
$ python-barcode create "My Text" outfile
New barcode saved as outfile.svg.
$ python-barcode create -t png "My Text" outfile
New barcode saved as outfile.png.
$ python-barcode create -b ean8 -t jpeg "1234567" ean8_out
New barcode saved as ean8_out.jpg.

Barcodes

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

Code 39

class barcode.codex.Code39(code, writer=None, add_checksum=True)

Initializes a new Code39 instance.

Parameters:
code : String

Code 39 string without * and checksum (added automatically if add_checksum is True).

writer : barcode.writer Instance

The writer to render the barcode (default: SVGWriter).

add_checksum : Boolean

Add the checksum to code or not (default: True).

get_fullcode()

Returns the full code, encoded in the barcode.

Returns:Full human readable code.
Return type:String
render(writer_options=Nonetext=None)

Renders the barcode using self.writer.

Parameters:
writer_options : Dict

Options for self.writer, see writer docs for details.

text : str

Text to render under the barcode.

Returns:

Output of the writers render method.

Class Hirarchy

Inheritance diagram of barcode.codex.Code39

Code 128

New in version 0.8beta1.

class barcode.codex.Code128(codewriter=None)

Initializes a new Code128 instance. The checksum is added automatically when building the bars.

Parameters:
code : String

Code 128 string without checksum (added automatically).

writer : barcode.writer Instance

The writer to render the barcode (default: SVGWriter).

get_fullcode()

Returns the full code, encoded in the barcode.

Returns:Full human readable code.
Return type:String
render(writer_options=Nonetext=None)

Renders the barcode using self.writer.

Parameters:
writer_options : Dict

Options for self.writer, see writer docs for details.

text : str

Text to render under the barcode.

Returns:

Output of the writers render method.

Class Hirachy

Inheritance diagram of barcode.codex.Code128

PZN

barcode.codex.PZN

alias of barcode.codex.PZN7

Class Hirarchy

Inheritance diagram of barcode.codex.PZN

EAN-13

class barcode.ean.EuropeanArticleNumber13(eanwriter=Noneno_checksum=False)

Initializes EAN13 object.

Parameters:
ean : String

The ean number as string.

writer : barcode.writer Instance

The writer to render the barcode (default: SVGWriter).

build()

Builds the barcode pattern from self.ean.

Returns:The pattern as string
Return type:String
calculate_checksum()

Calculates the checksum for EAN13-Code.

Returns:The checksum for self.ean.
Return type:Integer
get_fullcode()

Returns the full code, encoded in the barcode.

Returns:Full human readable code.
Return type:String
render(writer_options=Nonetext=None)

Renders the barcode using self.writer.

Parameters:
writer_options : Dict

Options for self.writer, see writer docs for details.

text : str

Text to render under the barcode.

Returns:

Output of the writers render method.

to_ascii()

Returns an ascii representation of the barcode.

Return type:String

Class Hirarchy

Inheritance diagram of barcode.ean.EuropeanArticleNumber13

EAN-8

class barcode.ean.EuropeanArticleNumber8(eanwriter=None)

Represents an EAN-8 barcode. See EAN13’s __init__ for details.

Parameters:
ean : String

The ean number as string.

writer : barcode.writer Instance

The writer to render the barcode (default: SVGWriter).

build()

Builds the barcode pattern from self.ean.

Returns:The pattern as string
Return type:String

Class Hirarchy

Inheritance diagram of barcode.ean.EuropeanArticleNumber8

JAN

class barcode.ean.JapanArticleNumber(janwriter=None)

Initializes JAN barcode.

Parameters:
jan : String

The jan number as string.

writer : barcode.writer Instance

The writer to render the barcode (default: SVGWriter).

Class Hirarchy

Inheritance diagram of barcode.ean.JapanArticleNumber

ISBN-13

class barcode.isxn.InternationalStandardBookNumber13(isbnwriter=None)

Initializes new ISBN-13 barcode.

Parameters:
isbn : String

The isbn number as string.

writer : barcode.writer Instance

The writer to render the barcode (default: SVGWriter).

Class Hirarchy

Inheritance diagram of barcode.isxn.InternationalStandardBookNumber13

ISBN-10

class barcode.isxn.InternationalStandardBookNumber10(isbnwriter=None)

Initializes new ISBN-10 barcode. This code is rendered as EAN-13 by prefixing it with 978.

Parameters:
isbn : String

The isbn number as string.

writer : barcode.writer Instance

The writer to render the barcode (default: SVGWriter).

Class Hirarchy

Inheritance diagram of barcode.isxn.InternationalStandardBookNumber10

ISSN

class barcode.isxn.InternationalStandardSerialNumber(issnwriter=None)

Initializes new ISSN barcode. This code is rendered as EAN-13 by prefixing it with 977 and adding 00 between code and checksum.

Parameters:
issn : String

The issn number as string.

writer : barcode.writer Instance

The writer to render the barcode (default: SVGWriter).

Class Hirarchy

Inheritance diagram of barcode.isxn.InternationalStandardSerialNumber

UPC-A

class barcode.upc.UniversalProductCodeA(upcwriter=Nonemake_ean=False)

Universal Product Code (UPC) barcode.

UPC-A consists of 12 numeric digits.

build()

Builds the barcode pattern from ‘self.upc’

Returns:The pattern as string
Return type:str
calculate_checksum()

Calculates the checksum for UPCA/UPC codes

Returns:The checksum for ‘self.upc’
Return type:int
get_fullcode()

Returns the full code, encoded in the barcode.

Returns:Full human readable code.
Return type:String
render(writer_options=Nonetext=None)

Renders the barcode using self.writer.

Parameters:
writer_options : Dict

Options for self.writer, see writer docs for details.

text : str

Text to render under the barcode.

Returns:

Output of the writers render method.

to_ascii()

Returns an ascii representation of the barcode.

Return type:str

Class Hirarchy

Inheritance diagram of barcode.upc.UniversalProductCodeA

Posts You May like

 


Most Viewed Posts

Write For Us

Name

Email *

Message *

All Blogs