Writer

Common Writer Options

All writer take the following options (specified as keyword arguments to Barcode.save(filename, option=value) or set via Writer.set_options(option=value)).

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_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 Base(initialize=None, paint_module=None, paint_text=None, finish=None)[source]

Base class for all writers.

Initializes the basic writer options. Sub-classes 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)[source]

Calculates the size of the barcode in pixel.

Parameters:
  • modules_per_line (int) – number of modules in one line.
  • number_of_lines (int) – number of lines of the barcode.
  • dpi (int) – DPI to calculate.
Returns:

Width and height of the barcode in pixel.

Return type:

(tuple[int, int])

register_callback(action, callback)[source]

Register one of the three callbacks if not given at instance creation.

Parameters:
  • action (str) – One of ‘initialize’, ‘paint_module’, ‘paint_text’, ‘finish’.
  • callback (function) – The callback function for the given action.
render(code)[source]

Renders the barcode to whatever the inheriting writer provides, using the registered callbacks.

Parameters:code (list[str]) – list of strings matching the writer spec (only contain 0 or 1).
save(filename, output)[source]

See Interface.save().

set_options(options)[source]

Sets the given options as instance attributes (only if they are known).

Parameters:options (dict) – All known instance attributes and more if the child class has defined them before this call.
class Interface[source]

Writer interface.

save(filename, content)[source]

Saves contents to filename.

Parameters:
  • filename (str) – filename without extension.
  • content (str) – output of rendering process.
Returns:

the filename.

Return type:

(str)

create_svg_object()[source]

Returns a blank SVG document.

Returns:XML document.
Return type:(xml.dom.minidom.DocumentType)
mm2px(value, dpi=300)[source]

Converts the given value in millimeters into dots per inch.

Parameters:
  • value (float) – value, in millimeters.
  • dpi (int) – resolution, in dots per inch.
Returns:

value, in dots per inch.

Return type:

(float)

pt2mm(value)[source]

Converts given value in points to millimeters.

Parameters:value (int) – value in points.
Returns:value, in millimeters.
Return type:(float)