Blabel Reference manual

LabelWriter

class blabel.LabelWriter(item_template_path=None, item_template=None, default_stylesheets=(), default_base_url=None, items_per_page=1, **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.

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.

**default_context

Use keywords to add ny variable, function, etc. that you are using in the templates.

Methods

record_to_html(record)

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

records_to_html(records[, target])

Build the full HTML document to be printed.

write_labels(records[, target, …])

Write the PDF document containing the labels to be printed.

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.

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

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

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
image_base64_data

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

blabel.label_tools.barcode(data, barcode_class='code128', fmt='png', **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
image_base64_data

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