ThermalPrinter API

Class

class thermalprinter.ThermalPrinter
__init__(port='/dev/ttyAMA0', baudrate=19200, **kwargs)
Parameters:
  • port (str) – Serial port to use, known as the device name.
  • baudrate (int) – Baud rate such as 9600 or 115200 etc.
  • **kwargs (dict) –

    Additionnal optional arguments:

    • heat_time (int): printer heat time (default: 80);
    • heat_interval (int): printer heat time interval (default: 12);
    • most_heated_point (int): for the printer, the most heated point (default: 3).
Raises:

ThermalPrinterValueError – On incorrect argument’s type or value.

Methods

Bar codes

class thermalprinter.ThermalPrinter
barcode(data, barecode_type) → None
Parameters:
  • data (mixed) – data to print.
  • barecode_type (BarCode) – bar code type to use.
Raises:

Bar code printing. All checks are done to ensure the data validity.

barcode_height(height=162) → None
Parameters:height (int) – bar code height (min=1, max=255).
Raises:ThermalPrinterValueError – On incorrect height’s type or value.

Set the bar code height.

barcode_left_margin(margin=0) → None
Parameters:margin (int) – left margin (min=0, max=255).
Raises:ThermalPrinterValueError – On incorrect margin’s type or value.

Set the left margin of the bar code.

barcode_position(position=BarCodePosition.HIDDEN) → None
Parameters:position (BarCodePosition) – the position to use.
Raises:ThermalPrinterConstantError – On bad position’s type.

Set the position of the text relative to the bar code.

barcode_width(width=3) → None
Parameters:width (int) – bar code with (min=2, max=6).
Raises:ThermalPrinterValueError – On incorrect width’s type or value.

Set the bar code width.

Images

class thermalprinter.ThermalPrinter
image(image) → None
Parameters:image (Image) – the image to print.
Raises:ThermalPrinterValueError – On bad image’s type.

Print Image. Requires Python Imaging Library. Image will be cropped to 384 pixels width if necessary, and converted to 1-bit w/diffusion dithering. For any other behavior (scale, B&W threshold, etc.), use the Imaging Library to perform such operations before passing the result to this function.

Max width: 384px.

>>> from PIL import Image
>>> printer.image(Image.open('picture.png'))

Text styling

class thermalprinter.ThermalPrinter
bold(state=False) → None
Parameters:state (bool) – new state.

Turn emphasized mode on/off.

char_spacing(spacing=0) → None
Parameters:spacing (int) – spacing to use (min=0, max=255).
Raises:ThermalPrinterValueError – On incorrect spacing’s type or value.

Set the right character spacing.

double_height(state=False) → None
Parameters:state (bool) – new state.

Set double height mode.

double_width(state=False) → None
Parameters:state (bool) – new state.

Select double width mode.

inverse(state=False) → None
Parameters:state (bool) – new state.

Turn white/black reverse printing mode.

justify(value='L') → None
Parameters:value (str) – the new justification.
Raises:ThermalPrinterValueError – On incorrect value’s type or value.

Set text justification:

  • left (L)
  • center (C)
  • right (R)
left_margin(margin=0) → None
Parameters:margin (int) – the new margin (min=0, max=47).
Raises:ThermalPrinterValueError – On incorrect margin’s type or value.

Set the left margin.

line_spacing(spacing=30) → None
Parameters:spacing (int) – the new spacing (min=0, max=255).
Raises:ThermalPrinterValueError – On incorrect spacing’s type or value.

Set line spacing.

rotate(state=False) → None
Parameters:state (bool) – new state.

Turn on/off clockwise rotation of 90°.

size(value='S') → None
Parameters:value (str) – the new text size.
Raises:ThermalPrinterValueError – On incorrect value’s type or value.

Set text size:

  • small (S)
  • medium: double height (M)
  • large: double width and height (L)

This method affects max_column.

strike(state=False) → None
Parameters:state (bool) – new state.

Turn on/off double-strike mode.

underline(weight=0) → None
Parameters:weight (int) – the underline’s weight (min=0, max=2).
Raises:ThermalPrinterValueError – On incorrect weight’s type or value.

Turn underline mode on/off:

  • 0: turns off underline mode
  • 1: turns on underline mode (1 dot thick)
  • 2: turns on underline mode (2 dots thick)
upside_down(state=False) → None
Parameters:state (bool) – new state.

Turns on/off upside-down printing mode.

Encoding and charsets

class thermalprinter.ThermalPrinter
charset(charset=CharSet.USA) → None
Parameters:charset (CharSet) – new charset to use.
Raises:ThermalPrinterConstantError – On bad charset’s type.

Select an internal character set.

codepage(codepage=CodePage.CP437) → None
Parameters:codepage (CodePage) – new code page to use.
Raises:ThermalPrinterConstantError – On bad codepage’s type.

Select character code table.

Chinese

class thermalprinter.ThermalPrinter
chinese(state=False) → None
Parameters:state (bool) – new state.

Select/cancel Chinese mode.

chinese_format(fmt=Chinese.GBK) → None
Parameters:fmt (Chinese) – new format to use.
Raises:ThermalPrinterConstantError – On bad fmt’s type.

Selection of the Chinese format.

Printing

class thermalprinter.ThermalPrinter
feed(number=1) → None
Parameters:number (int) – number of lines.
Raises:ThermalPrinterValueError – On incorrect number’s type or value.

Feeds by the specified number of lines (min=0, max=255).

out(data, line_feed=True, **kwargs) → None
Parameters:
  • data (mixed) – the data to print.
  • line_feed (bool) – send a line break after the printed data.
  • **kwargs (dict) – additional styles to apply.

Send a line to the printer.

You can pass formatting instructions directly via arguments:

>>> printer.out(data, justify='C', inverse=True)

This will prevent you to do:

>>> printer.justify('C')
>>> printer.inverse(True)
>>> printer.out(data)
>>> printer.inverse(False)
>>> printer.justify('L')

Printer state

class thermalprinter.ThermalPrinter
offline() → None

Take the printer offline. Print commands sent after this will be ignored until offline() is called.

online() → None

Take the printer online. Subsequent print commands will be obeyed.

sleep(seconds=1) → None
Parameters:seconds (int) – value to pass to the printer (min=0, unit=sec).
Raises:ThermalPrinterValueError – On incorrect seconds’s type or value.

Put the printer into a low-energy state.

status() → dict

Check the printer status. If RX pin is not connected, all values will be set to True.

Return a dict:

  • movement: False if the movement is not connected;
  • paper: False is no paper;
  • temp: False if the temperature exceeds 60°C;
  • voltage: False if the voltage is higher than 9.5V.
reset() → None

Reset the printer to factory defaults.

test() → None

Print the test page (contains printer’s settings).

wake() → None

Wake up the printer.

Special methods

class thermalprinter.ThermalPrinter
send_command(*args) → None
Parameters:*args (list) – command and arguments for the printer.

Raw byte-writing.

to_bytes(data) → bytes
Parameters:data (mixed) – any type of data to print.

Convert data before sending to the printer.

Attributes

All these attributes are read-only.

class thermalprinter.ThermalPrinter
feeds
Getter:Number of printed line feeds since the start of the script.
Type:int
Raises:ThermalPrinterAttributeError – When trying to assign a value.
is_online
Getter:The printer is online.
Type:bool
Raises:ThermalPrinterAttributeError – When trying to assign a value.
is_sleeping
Getter:The printer is sleeping.
Type:bool
Raises:ThermalPrinterAttributeError – When trying to assign a value.
lines
Getter:Number of printed lines since the start of the script.
Type:int
Raises:ThermalPrinterAttributeError – When trying to assign a value.
max_column
Getter:Number of printable characters on one line.
Type:int
Raises:ThermalPrinterAttributeError – When trying to assign a value.

Exceptions

exception thermalprinter.ThermalPrinterError

Base class for thermal printer exceptions.

exception thermalprinter.ThermalPrinterAttributeError

Exception that is raised when trying to assign something to a read-only attribute.

exception thermalprinter.ThermalPrinterConstantError

Exception that is raised on inexistant or out of range constant.

exception thermalprinter.ThermalPrinterValueError

Exception that is raised on incorrect type or value passed to any method.

Constants

Bar codes types

BarCode.UPC_A
BarCode.UPC_E
BarCode.JAN13
BarCode.JAN8
BarCode.CODE39
BarCode.ITF
BarCode.CODABAR
BarCode.CODE93
BarCode.CODE128

Bar codes positions

BarCodePosition.HIDDEN
BarCodePosition.ABOVE
BarCodePosition.BELOW
BarCodePosition.BOTH

Characters sets

CharSet.USA
CharSet.FRANCE
CharSet.GERMANY
CharSet.UK
CharSet.DENMARK
CharSet.SWEDEN
CharSet.ITALY
CharSet.SPAIN
CharSet.JAPAN
CharSet.NORWAY
CharSet.DENMARK2
CharSet.SPAIN2
CharSet.LATIN_AMERICAN
CharSet.KOREA
CharSet.SLOVENIA
CharSet.CHINA

Chinese formats

Chinese.GBK
Chinese.UTF_8
Chinese.BIG5

Code pages

CodePage.CP437

the United States of America, European standard

CodePage.CP932

Katakana

CodePage.CP850

Multi language

CodePage.CP860

Portuguese

CodePage.CP863

Canada, French

CodePage.CP865

Western Europe

CodePage.CYRILLIC

The Slavic language

CodePage.CP866

The Slavic 2

CodePage.MIK

The Slavic / Bulgaria

CodePage.CP755

Eastern Europe, Latvia 2

CodePage.IRAN

Iran, Persia

CodePage.CP862

Hebrew

CodePage.CP1252

Latin 1 [WCP1252]

CodePage.CP1253

Greece [WCP1253]

CodePage.CP852

Latina 2

CodePage.CP858

A variety of language Latin 1 + Europe

CodePage.IRAN2

Persian

CodePage.LATVIA
CodePage.CP864

Arabic

CodePage.ISO_8859_1

Western Europe

CodePage.CP737

Greece

CodePage.CP1257

The Baltic Sea

CodePage.THAI

Thai Wen

CodePage.CP720

Arabic

CodePage.CP855
CodePage.CP857

Turkish

CodePage.CP1250

Central Europe [WCP1250]

CodePage.CP775
CodePage.CP1254

Turkish [WCP1254]

CodePage.CP1255

Hebrew [WCP1255]

CodePage.CP1256

Arabic [WCP1256]

CodePage.CP1258

Vietnamese [WCP1258]

CodePage.ISO_8859_2

Latin 2

CodePage.ISO_8859_3

Latin 3

CodePage.ISO_8859_4

Baltic languages

CodePage.ISO_8859_5

The Slavic language

CodePage.ISO_8859_6

Arabic

CodePage.ISO_8859_7

Greece

CodePage.ISO_8859_8

Hebrew

CodePage.ISO_8859_9

Turkish

CodePage.ISO_8859_15

: Latin 9

CodePage.THAI2

Thai Wen 2

CodePage.CP856
CodePage.CP874

Code pages fallback

Certain code pages are not available on Python, so we use a little translation table. If you find a better fit for one of the code page below, open an issue please (or send a patch).

CodePageConverted.MIK

ISO8859-5

CodePageConverted.CP755

UTF-8

CodePageConverted.IRAN

CP1256

CodePageConverted.IRAN2

UTF-8

CodePageConverted.LATVIA

ISO8859-4

CodePageConverted.THAI

ISO8859-11

CodePageConverted.THAI2

UTF-8