Blabel reference manual

LabelWriter

class blabel.LabelWriter(item_template_path=None, item_template=None, default_stylesheets=(), default_base_url=None, items_per_page=1, encoding=None, **default_context)[source]

Class to write labels.

Parameters:
  • item_template_path – Path to an HTML/jinja2 html template file that will serve as template for translating a dict record into the HTML of one item. Alternatively an item_template parameter can be provided. Set encoding of the file with the encoding parameter (e.g. utf-8).

  • item_template – jinja2.Template object to serve as a template for translating a dict record into the HTML of one item.

  • default_stylesheets – List of weasyprint.CSS objects or path to .css spreadsheets to be used for default styling.

  • default_base_url – Path to use as origin for relative paths in the HTML document. (Only useful when using assets such as images etc.)

  • items_per_page – Number of items per page (= per sticker). This is particularly practical if you only have “landscape” stickers and want to print square labels by printing 2 labels per stickers and cutting the stickers in two afterwards.

  • encoding – The encoding of the item template file.

  • **default_context – Use keywords to add any variable, function, etc. that you are using in the templates.

record_to_html(record)[source]

Convert one record to an html string using the item template.

records_to_html(records, target=None)[source]

Build the full HTML document to be printed.

If target is None, the raw HTML string is returned, else the HTML is written at the path specified by target.

write_labels(records, target=None, extra_stylesheets=(), base_url=None)[source]

Write the PDF document containing the labels to be printed.

Parameters:
  • records – List of dictionaries with the parameters of each label to print.

  • target – Path of the PDF file to be generated. If left to None or “@memory”, the raw file data will be returned.

  • extra_stylesheets – List of path to stylesheets of Weasyprint CSS objects to complement the default stylesheets.

  • base_url – Path of the origin for the different relative path inside the HTML document getting printed.

Tools

Utilities for label generation.

blabel.label_tools.now(fmt='%Y-%m-%d %H:%M')[source]

Display the current time.

Default format is “year-month-day hour:minute” but another format can be provided (see datetime docs for date formatting).

blabel.label_tools.pil_to_html_imgdata(img, fmt='PNG')[source]

Convert a PIL image into HTML-displayable data.

The result is a string data:image/FMT;base64,xxxxxxxxx which you can provide as a “src” parameter to a <img/> tag.

Examples

>>> data = pil_to_html_imgdata(my_pil_img)
>>> html_data = '<img src="%s"/>' % data
blabel.label_tools.wrap(text, col_width)[source]

Breaks the text into lines with at maximum ‘col_width’ characters.

blabel.label_tools.hiro_square(width='100%')[source]

Return a <svg/> string of a Hiro square to be included in HTML.

blabel.label_tools.qr_code(data, optimize=20, fill_color='black', back_color='white', **qr_code_params)[source]

Return a QR code’s image data.

Powered by the Python library qrcode. See this library’s documentation for more details.

Parameters:
  • data – Data to be encoded in the QR code.

  • optimize – Chunk length optimization setting.

  • fill_color – Colors to use for QRcode and its background.

  • back_color – Colors to use for QRcode and its background.

  • **qr_code_params – Parameters of the qrcode.QRCode constructor, such as version, error_correction, box_size, border.

Returns:

A string data:image/png;base64,xxxxxxxxx which you can provide as a “src” parameter to a <img/> tag.

Return type:

image_base64_data

Examples

>>> data = qr_code('egf45728')
>>> html_data = '<img src="%s"/>' % data
blabel.label_tools.datamatrix(data, cellsize=2, with_border=False)[source]

Return a datamatrix’s image data.

Powered by the Python library pyStrich. See this library’s documentation for more details.

Parameters:
  • data – Data to be encoded in the datamatrix.

  • cellsize – size of the picture in inches (?).

  • with_border – If false, there will be no border or margin to the datamatrix image.

Returns:

A string data:image/png;base64,xxxxxxxxx which you can provide as a “src” parameter to a <img/> tag.

Return type:

image_base64_data

Examples

>>> data = datamatrix('EGF')
>>> html_data = '<img src="%s"/>' % data
blabel.label_tools.barcode(data, barcode_class='code128', fmt='png', add_checksum=True, **writer_options)[source]

Return a barcode’s image data.

Powered by the Python library python-barcode. See this library’s documentation for more details.

Parameters:
  • data – Data to be encoded in the datamatrix.

  • barcode_class – Class/standard to use to encode the data. Different standards have different constraints.

  • writer_options – Various options for the writer to tune the appearance of the barcode (see python-barcode documentation).

Returns:

A string data:image/png;base64,xxxxxxxxx which you can provide as a “src” parameter to a <img/> tag.

Return type:

image_base64_data

Examples

>>> data = barcode('EGF12134', barcode_class='code128')
>>> html_data = '<img src="%s"/>' % data

Examples of writer options:

>>> { 'background': 'white',
>>>   'font_size': 10,
>>>   'foreground': 'black',
>>>   'module_height': 15.0,
>>>   'module_width': 0.2,
>>>   'quiet_zone': 6.5,
>>>   'text': '',
>>>   'text_distance': 5.0,
>>>   'write_text': True
>>> }