python-barcode Writer

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

API (autogenerated)

class barcode.writer.BaseWriter(initialize=None, paint_module=None, paint_text=None, finish=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: callback_initialize(raw_code)

paint_module : Function

Callback for painting one barcode module. Is called: callback_paint_module(xpos, ypos, width, color)

paint_text : Function

Callback for painting the text under the barcode. Is called: callback_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).

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.