0% found this document useful (0 votes)
139 views

Pybarcode Documentation: Release 0.13.1

python-barcode-readthedocs-io-en-stable

Uploaded by

Vicmont Skylle
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views

Pybarcode Documentation: Release 0.13.1

python-barcode-readthedocs-io-en-stable

Uploaded by

Vicmont Skylle
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

pyBarcode Documentation

Release 0.13.1

Thorsten Weimann

Apr 07, 2021


Contents

1 Contents 3
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Create barcodes from the commandline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 Provided barcodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 python-barcode Writer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.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, the
images 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 script
detects your Python version and adds the major version number to the executable script.
Usage:

$ 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.

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 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

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.PZN
alias 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 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

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 via
Writer.set_options(options), where options is a dictionary where keys are option names and values are option values
to 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 mm
as float. Defaults to 6.5.
font_path Path to the font file to be used. Defaults to DejaVuSansMono (which is bundled with this
package).
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) and
finish().
Now instatiate a new barcode and give an instance of your new writer as argument. If you now call render on the
barcode 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 using
self.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 them
before 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 al


Copyright (c) 2010-2013 Thorsten Weimann

Permission is hereby granted, free of charge, to any person obtaining a copy


of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, 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 IN
THE SOFTWARE.

14 Chapter 1. Contents
Python Module Index

b
barcode.writer, 12

15
pyBarcode Documentation, Release 0.13.1

16 Python Module Index


Index

B I
barcode.writer (module), 12 ImageWriter (class in barcode.writer), 13
BaseWriter (class in barcode.writer), 12 InternationalStandardBookNumber10 (class
build() (barcode.ean.EuropeanArticleNumber13 in barcode.isxn), 8
method), 6 InternationalStandardBookNumber13 (class
build() (barcode.ean.EuropeanArticleNumber8 in barcode.isxn), 8
method), 7 InternationalStandardSerialNumber (class
build() (barcode.upc.UniversalProductCodeA in barcode.isxn), 9
method), 9
J
C JapanArticleNumber (class in barcode.ean), 8
calculate_checksum() (bar-
code.ean.EuropeanArticleNumber13 method), P
6 PZN (in module barcode.codex), 6
calculate_checksum() (bar-
code.upc.UniversalProductCodeA method), R
9 register_callback() (barcode.writer.BaseWriter
calculate_size() (barcode.writer.BaseWriter method), 12
method), 12 render() (barcode.codex.Code128 method), 5
Code128 (class in barcode.codex), 5 render() (barcode.codex.Code39 method), 4
Code39 (class in barcode.codex), 4 render() (barcode.ean.EuropeanArticleNumber13
method), 7
E render() (barcode.upc.UniversalProductCodeA
EuropeanArticleNumber13 (class in barcode.ean), method), 10
6 render() (barcode.writer.BaseWriter method), 12
EuropeanArticleNumber8 (class in barcode.ean),
7 S
save() (barcode.writer.BaseWriter method), 12
G save() (barcode.writer.ImageWriter method), 13
get_fullcode() (barcode.codex.Code128 method), save() (barcode.writer.SVGWriter method), 13
5 set_options() (barcode.writer.BaseWriter method),
get_fullcode() (barcode.codex.Code39 method), 4 13
get_fullcode() (bar- SVGWriter (class in barcode.writer), 13
code.ean.EuropeanArticleNumber13 method),
6 T
get_fullcode() (bar- to_ascii() (barcode.ean.EuropeanArticleNumber13
code.upc.UniversalProductCodeA method), method), 7
9 to_ascii() (barcode.upc.UniversalProductCodeA
method), 10

17
pyBarcode Documentation, Release 0.13.1

U
UniversalProductCodeA (class in barcode.upc), 9

W
write() (barcode.writer.ImageWriter method), 13
write() (barcode.writer.SVGWriter method), 13
writer, 10
writer_options, 10

18 Index

You might also like