pyBarcode DocumentationRelease 0.13.1
Thorsten Weimann
Apr 07, 2021
Contents
1 Contents 31.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.2 Create barcodes from the commandline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.3 Provided barcodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.4 python-barcode Writer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101.5 License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Python Module Index 15
Index 17
i
ii
pyBarcode Documentation, Release 0.13.1
Contents 1
pyBarcode Documentation, Release 0.13.1
2 Contents
CHAPTER 1
Contents
1.1 Introduction
This package was created to have barcodes available with pure-python. Pillow is required for exporting images (png,jpg), although not for SVGs.
All you need to create a barcode is to know the system (EAN, UPC, . . . ) and the code (e.g. for EAN-13:123456789102). As you see, you do not need the checksum, it will be calculated automatically. In some systems(Code 39) the checksum is optional, there you can give the add_checksum keyword argument (default is True).
1.1.1 Creating barcodes as SVG
To generate barcodes as SVG objects, you can use the default writer (simply not specify a writer).
Quick example:
>>> 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'
Now you have ean13.svg and the compressed ean13.svgz in your current working directory. Open it and see the result.
3
pyBarcode Documentation, Release 0.13.1
1.1.2 Creating barcodes as Image
New in version 0.4b1.
To generate barcodes as images, you must provide the ImageWriter to the get function. Without any options, theimages are rendered as PNG.
Quick example:
>>> import barcode>>> from barcode.writer import ImageWriter>>> ean = barcode.get('ean13', '123456789102', writer=ImageWriter())>>> filename = ean.save('ean13')>>> filename'ean13.png'
1.2 Create barcodes from the commandline
New in version 0.7beta4.
python-barcode ships with a little commandline script to generate barcodes without knowing Python. The install scriptdetects your Python version and adds the major version number to the executable script.
Usage:
$ python-barcode create "My Text" outfileNew barcode saved as outfile.svg.$ python-barcode create -t png "My Text" outfileNew barcode saved as outfile.png.$ python-barcode create -b ean8 -t jpeg "1234567" ean8_outNew barcode saved as ean8_out.jpg.
See python-barcode -h for more options.
1.3 Provided barcodes
1.3.1 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 isTrue).
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
4 Chapter 1. Contents
pyBarcode Documentation, Release 0.13.1
render(writer_options=None, text=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
Barcode Code39
1.3.2 Code 128
New in version 0.8beta1.
class barcode.codex.Code128(code, writer=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=None, text=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.
1.3. Provided barcodes 5
pyBarcode Documentation, Release 0.13.1
Class Hirachy
Barcode Code128
1.3.3 PZN
barcode.codex.PZNalias of barcode.codex.PZN7
Class Hirarchy
Barcode Code39 PZN7
1.3.4 EAN-13
class barcode.ean.EuropeanArticleNumber13(ean, writer=None, no_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.
6 Chapter 1. Contents
pyBarcode Documentation, Release 0.13.1
Return type String
render(writer_options=None, text=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
Barcode EuropeanArticleNumber13
1.3.5 EAN-8
class barcode.ean.EuropeanArticleNumber8(ean, writer=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
Barcode EuropeanArticleNumber13 EuropeanArticleNumber8
1.3. Provided barcodes 7
pyBarcode Documentation, Release 0.13.1
1.3.6 JAN
class barcode.ean.JapanArticleNumber(jan, writer=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
Barcode EuropeanArticleNumber13 JapanArticleNumber
1.3.7 ISBN-13
class barcode.isxn.InternationalStandardBookNumber13(isbn, writer=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
Barcode EuropeanArticleNumber13 InternationalStandardBookNumber13
1.3.8 ISBN-10
class barcode.isxn.InternationalStandardBookNumber10(isbn, writer=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).
8 Chapter 1. Contents
pyBarcode Documentation, Release 0.13.1
Class Hirarchy
Barcode EuropeanArticleNumber13 InternationalStandardBookNumber13 InternationalStandardBookNumber10
1.3.9 ISSN
class barcode.isxn.InternationalStandardSerialNumber(issn, writer=None)Initializes new ISSN barcode. This code is rendered as EAN-13 by prefixing it with 977 and adding 00 betweencode and checksum.
Parameters
issn [String] The issn number as string.
writer [barcode.writer Instance] The writer to render the barcode (default: SVGWriter).
Class Hirarchy
Barcode EuropeanArticleNumber13 InternationalStandardSerialNumber
1.3.10 UPC-A
class barcode.upc.UniversalProductCodeA(upc, writer=None, make_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.
1.3. Provided barcodes 9
pyBarcode Documentation, Release 0.13.1
Returns Full human readable code.
Return type String
render(writer_options=None, text=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
Barcode UniversalProductCodeA
1.4 python-barcode Writer
1.4.1 Common Writer Options
All writer take the following options (specified as keyword arguments to Barcode.save(filename, options) or set viaWriter.set_options(options), where options is a dictionary where keys are option names and values are option valuesto be set).
Note: See the documentation of the specific writer for special options, only available for this writer.
Common Options:
module_width The width of one barcode module in mm as float. Defaults to 0.2.
module_height The height of the barcode modules in mm as float. Defaults to 15.0.
quiet_zone Distance on the left and on the right from the border to the first (last) barcode module in mmas float. Defaults to 6.5.
font_path Path to the font file to be used. Defaults to DejaVuSansMono (which is bundled with thispackage).
font_size Font size of the text under the barcode in pt as integer. Defaults to 10.
10 Chapter 1. Contents
pyBarcode Documentation, Release 0.13.1
text_distance Distance between the barcode and the text under it in mm as float. Defaults to 5.0.
background The background color of the created barcode as string. Defaults to white.
foreground The foreground and text color of the created barcode as string. Defaults to black.
New in version 0.6.
center_text If true (the default) the text is centered under the barcode else left aligned.
Note: Some barcode classes change the above defaults to fit in some kind of specification.
1.4.2 Writers
python-barcode SVGWriter
Creates barcodes as (compressed) SVG objects.
Special Options
In addition to the common writer options you can give the following special option.
Special Option:
compress Boolean value to output a compressed SVG object (.svgz). Defaults to False.
python-barcode ImageWriter
New in version 0.4b1.
Creates barcodes as image. All imagetypes supported by Pillow are available.
Special Options
In addition to the common writer options you can give the following special options.
Special Options:
format The image file format as string. All formats supported by Pillow are valid (e.g. PNG, JPEG,BMP, . . . ). Defaults to PNG.
dpi DPI as integer to calculate the image size in pixel. This value is used for all mm to px calculations.Defaults to 300.
1.4. python-barcode Writer 11
pyBarcode Documentation, Release 0.13.1
Create your own writer
To create your own writer, inherit from barcode.writer.BaseWriter. In your __init__ method call BaseWriter’s __init__and give your callbacks for initialize(raw_code), paint_module(xpos, ypos, width, color), paint_text(xpos, ypos) andfinish().
Now instatiate a new barcode and give an instance of your new writer as argument. If you now call render on thebarcode instance your callbacks get called.
1.4.3 API (autogenerated)
class barcode.writer.BaseWriter(initialize=None, paint_module=None, paint_text=None, fin-ish=None)
Baseclass for all writers.
Initializes the basic writer options. Childclasses can add more attributes and can set them directly or usingself.set_options(option=value).
Parameters
initialize [Function] Callback for initializing the inheriting writer. Is called: call-back_initialize(raw_code)
paint_module [Function] Callback for painting one barcode module. Is called: call-back_paint_module(xpos, ypos, width, color)
paint_text [Function] Callback for painting the text under the barcode. Is called: call-back_paint_text(xpos, ypos) using self.text as text.
finish [Function] Callback for doing something with the completely rendered output. Is called:return callback_finish() and must return the rendered output.
calculate_size(modules_per_line, number_of_lines, dpi=300)Calculates the size of the barcode in pixel.
Parameters
modules_per_line [Integer] Number of modules in one line.
number_of_lines [Integer] Number of lines of the barcode.
dpi [Integer] DPI to calculate.
Returns Width and height of the barcode in pixel.
Return type Tuple
register_callback(action, callback)Register one of the three callbacks if not given at instance creation.
Parameters
action [String] One of ‘initialize’, ‘paint_module’, ‘paint_text’, ‘finish’.
callback [Function] The callback function for the given action.
render(code)Renders the barcode to whatever the inheriting writer provides, using the registered callbacks.
Parameters
code [List] List of strings matching the writer spec (only contain 0 or 1).
12 Chapter 1. Contents
pyBarcode Documentation, Release 0.13.1
save(filename, output)Saves the rendered output to filename.
Parameters
filename [String] Filename without extension.
output [String] The rendered output.
Returns The full filename with extension.
Return type String
set_options(options)Sets the given options as instance attributes (only if they are known).
Parameters
options [Dict] All known instance attributes and more if the childclass has defined thembefore this call.
Return type None
class barcode.writer.ImageWriter(format=’PNG’, mode=’RGB’)
save(filename, output)Saves the rendered output to filename.
Parameters
filename [String] Filename without extension.
output [String] The rendered output.
Returns The full filename with extension.
Return type String
write(content, fp: BinaryIO)Write content into a file-like object.
Content should be a barcode rendered by this writer.
class barcode.writer.SVGWriter
save(filename, output)Saves the rendered output to filename.
Parameters
filename [String] Filename without extension.
output [String] The rendered output.
Returns The full filename with extension.
Return type String
write(content, fp: BinaryIO)Write content into a file-like object.
Content should be a barcode rendered by this writer.
1.4. python-barcode Writer 13
pyBarcode Documentation, Release 0.13.1
1.5 License
python-barcode is distributed under the MIT license:
Copyright (c) 2017-2020 Hugo Osvaldo Barrera <[email protected]>, et alCopyright (c) 2010-2013 Thorsten Weimann
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS INTHE SOFTWARE.
14 Chapter 1. Contents
Python Module Index
bbarcode.writer, 12
15
pyBarcode Documentation, Release 0.13.1
16 Python Module Index
Index
Bbarcode.writer (module), 12BaseWriter (class in barcode.writer), 12build() (barcode.ean.EuropeanArticleNumber13
method), 6build() (barcode.ean.EuropeanArticleNumber8
method), 7build() (barcode.upc.UniversalProductCodeA
method), 9
Ccalculate_checksum() (bar-
code.ean.EuropeanArticleNumber13 method),6
calculate_checksum() (bar-code.upc.UniversalProductCodeA method),9
calculate_size() (barcode.writer.BaseWritermethod), 12
Code128 (class in barcode.codex), 5Code39 (class in barcode.codex), 4
EEuropeanArticleNumber13 (class in barcode.ean),
6EuropeanArticleNumber8 (class in barcode.ean),
7
Gget_fullcode() (barcode.codex.Code128 method),
5get_fullcode() (barcode.codex.Code39 method), 4get_fullcode() (bar-
code.ean.EuropeanArticleNumber13 method),6
get_fullcode() (bar-code.upc.UniversalProductCodeA method),9
IImageWriter (class in barcode.writer), 13InternationalStandardBookNumber10 (class
in barcode.isxn), 8InternationalStandardBookNumber13 (class
in barcode.isxn), 8InternationalStandardSerialNumber (class
in barcode.isxn), 9
JJapanArticleNumber (class in barcode.ean), 8
PPZN (in module barcode.codex), 6
Rregister_callback() (barcode.writer.BaseWriter
method), 12render() (barcode.codex.Code128 method), 5render() (barcode.codex.Code39 method), 4render() (barcode.ean.EuropeanArticleNumber13
method), 7render() (barcode.upc.UniversalProductCodeA
method), 10render() (barcode.writer.BaseWriter method), 12
Ssave() (barcode.writer.BaseWriter method), 12save() (barcode.writer.ImageWriter method), 13save() (barcode.writer.SVGWriter method), 13set_options() (barcode.writer.BaseWriter method),
13SVGWriter (class in barcode.writer), 13
Tto_ascii() (barcode.ean.EuropeanArticleNumber13
method), 7to_ascii() (barcode.upc.UniversalProductCodeA
method), 10
17
pyBarcode Documentation, Release 0.13.1
UUniversalProductCodeA (class in barcode.upc), 9
Wwrite() (barcode.writer.ImageWriter method), 13write() (barcode.writer.SVGWriter method), 13writer, 10writer_options, 10
18 Index