π APIΒΆ
ClassΒΆ
- class thermalprinter.ThermalPrinter(port: str = '/dev/ttyAMA0', *, baudrate: int = 19200, byte_time: float = 5e-05, command_timeout: float = 0.05, dot_feed_time: float = 0.0021, dot_print_time: float = 0.03, heat_interval: int = 2, heat_time: int = 80, most_heated_point: int = 9, read_timeout: float = 0.0, run_setup_cmd: bool = True, use_stats: bool = True, write_timeout: float = 1.0)ΒΆ
The class managing the thermal printer.
- Parameters:
port (str ) β Serial port to use, known as the device name (see
constants.Defaults.PORT).baudrate (int ) β Baud rate (see
constants.Defaults.BAUDRATE).byte_time (float ) β Number of seconds to issue one byte to the printer. 11 bits (not 8) to accommodate idle, start, and stop, bits. See
constants.Defaults.BYTE_TIME.command_timeout (float ) β Time to sleep after issuing a command to the printer, in seconds.
dot_feed_time (float ) β Printer feed time, in seconds (see
constants.Defaults.DOT_FEED_TIME).dot_print_time (float ) β Printer dot time, in seconds (see
constants.Defaults.DOT_PRINT_TIME).heat_interval (int ) β Printer heat time interval (see
constants.Defaults.HEAT_INTERVAL).heat_time (int ) β Printer heat time (see
constants.Defaults.HEAT_TIME).most_heated_point (int ) β Printer most heated point (see
constants.Defaults.MOST_HEATED_POINT).read_timeout (float ) β Serial read timeout, in seconds (see
constants.Defaults.READ_TIMEOUT).run_setup_cmd (bool ) β Set to
Falseto disable the automatic one-shot run of the printer settings command (that ay be problematic on some devices).use_stats (bool ) β Set to
Falseto disable statistics persistence. See tools for its usage.write_timeout (float ) β Serial write timeout, in seconds (see
constants.Defaults.WRITE_TIMEOUT).
- Raises:
ThermalPrinterValueError β On incorrect argumentβs type, or value.
Added in version 0.3.0: The
command_timeoutkeyword-argument.Added in version 1.0.0:
byte_time,dot_feed_time,dot_print_time,run_setup_cmd,read_timeout,use_stats, andwrite_timeout, keyword-arguments.
MethodsΒΆ
PrintingΒΆ
- ThermalPrinter.feed(number: int = 1) None ΒΆ
Feed by the specified number of lines.
- Parameters:
number (int ) β The number of lines (min=0, max=255).
- Raises:
ThermalPrinterValueError β On incorrect
numberβs type, or value.
- ThermalPrinter.out(data: Any, line_feed: bool = True, **kwargs: Any) None ΒΆ
Send one line to the printer.
- Parameters:
You can pass formatting instructions directly via arguments:
>>> printer.out(data, justify=Justify.CENTER, inverse=True)This is a quicker way to do:
>>> printer.justify(Justify.CENTER) >>> printer.inverse(True) >>> printer.out(data) >>> printer.inverse(False) >>> printer.justify(Justify.LEFT)
Hint
A special boolean keyword-argument can be used to print Persian text:
persian=True. It will reshapedata, and set proper text styles to issue the final text.See recipes for required dependencies.
Added in version 1.0.0: The
persiankeyword-argument.
BarcodesΒΆ
- ThermalPrinter.barcode(data: str , barcode_type: BarCode, **kwargs: Any) None ΒΆ
Barcode printing. All checks are done to ensure the data validity.
- Parameters:
- Raises:
ThermalPrinterValueError β On incorrect
dataβs type, or value.
You can set additional barcode properties via arguments:
>>> printer.barcode( ... "012345678901", ... BarCode.EAN13, ... width=3, ... height=80, ... left_margin=2, ... position=BarCodePosition.BELOW)
This is a quicker way to do:
>>> printer.barcode_width(3) >>> printer.barcode_height(80) >>> printer.barcode_left_margin(2) >>> printer.barcode_position(BarCodePosition.BELOW) >>> printer.barcode("012345678901", BarCode.EAN13)
Added in version 1.0.0: The
kwargskeyword-argument to set additional barcode properties.
- ThermalPrinter.barcode_height(height: int = 162) None ΒΆ
Set the barcode height.
- Parameters:
height (int ) β The barcode height (min=1, max=255).
- Raises:
ThermalPrinterValueError β On incorrect
heightβs type, or value.
- ThermalPrinter.barcode_left_margin(margin: int = 0) None ΒΆ
Set the left margin of the barcode.
- Parameters:
margin (int ) β The barcode left margin (min=0, max=255).
- Raises:
ThermalPrinterValueError β On incorrect
marginβs type, or value.
- ThermalPrinter.barcode_position(position: BarCodePosition = BarCodePosition.HIDDEN) None ΒΆ
Set the position of the text relative to the barcode.
- Parameters:
position (BarCodePosition) β The barcode position to use.
- ThermalPrinter.barcode_width(width: int = 3) None ΒΆ
Set the barcode width.
- Parameters:
width (int ) β The barcode with (min=2, max=6).
- Raises:
ThermalPrinterValueError β On incorrect
widthβs type, or value.
- static ThermalPrinter.validate_barcode(data: str , barcode_type: BarCode) None ΒΆ
Validate data against the barcode type.
- Parameters:
- Raises:
ThermalPrinterValueError β On incorrect
dataβs type, or value.
Added in version 1.0.0.
ImagesΒΆ
- ThermalPrinter.image(image: Any) None ΒΆ
Picture printing.
Requires the Python Imaging Library (Pillow). The image will be resized to 384 pixels width (
constants.MAX_IMAGE_WIDTH) if necessary, and converted to 1-bit without diffusion dithering.- Parameters:
image (str | pathlib.Path | PIL.Image) β The file, or PIL Image object, to print.
Examples:
>>> printer.image("picture.png")>>> from pathlib import Path >>> printer.image(Path.home() / "picture.png")
>>> from PIL import Image >>> printer.image(Image.open("picture.png"))
Changed in version 1.0.0:
imagecan also be astr, orpathlib.Path, in addition to the originalPIL.Imageobject.Tip
Since v1.0.0 the image will be automatically resized when too wide.
Important
It works better with white background instead of transparence.
- ThermalPrinter.image_chunks(image: Any) bytearray ΒΆ
TODO Convert a given
imageto 1-bit without diffusion dithering, if necessary.- Parameters:
image (PIL.Image) β The PIL Image object to handle.
- Return type:
- Returns:
The converted image object, if converted, else the original
image.
Hint
Usually you do not need to call this method manually. It is used automatically by the
image()method.Added in version 1.0.0.
- ThermalPrinter.image_convert(image: Any) AnyΒΆ
Convert a given
imageto 1-bit without diffusion dithering, if necessary.- Parameters:
image (PIL.Image) β The PIL Image object to convert.
- Return type:
- Returns:
The converted image object, if converted, else the original
image.
Hint
Usually you do not need to call this method manually. It is used automatically by the
image()method.Added in version 1.0.0.
- ThermalPrinter.image_resize(image: Any) AnyΒΆ
Resize a given
imageto fit into the maximum width of 384 pixels (constants.MAX_IMAGE_WIDTH), if necessary. The size proportion will be respected.- Parameters:
image (PIL.Image) β The PIL Image object to resize.
- Return type:
- Returns:
The resized image object, if resized, else the original
image.
Hint
Usually you do not need to call this method manually. It is used automatically by the
image()method.Added in version 1.0.0.
Text StylingΒΆ
- ThermalPrinter.bold(state: bool = False) None ΒΆ
Turn on/off the emphasized mode.
- Parameters:
state (bool ) β Enabled if
stateisTrue, else disabled.
- ThermalPrinter.char_spacing(spacing: int = 0) None ΒΆ
Set the character spacing.
- Parameters:
spacing (int ) β The spacing to use (min=0, max=255).
- Raises:
ThermalPrinterValueError β On incorrect
spacingβs type, or value.
- ThermalPrinter.double_height(state: bool = False) None ΒΆ
Turn on/off the double height mode.
- Parameters:
state (bool ) β Enabled if
stateisTrue, else disabled.
- ThermalPrinter.double_width(state: bool = False) None ΒΆ
Turn on/off the double width mode.
- Parameters:
state (bool ) β Enabled if
stateisTrue, else disabled.
- ThermalPrinter.font_b(state: bool = False) None ΒΆ
Turn on/off the font B mode.
- Parameters:
state (bool ) β Enabled if
stateisTrue, else disabled.
Added in version 1.0.0.
- ThermalPrinter.inverse(state: bool = False) None ΒΆ
Turn on/off the white/black reverse printing mode.
- Parameters:
state (bool ) β Enabled if
stateisTrue, else disabled.
- ThermalPrinter.justify(value: Justify = Justify.LEFT) None ΒΆ
Set the text justification.
- Parameters:
value (Justify) β The justification to use.
Changed in version 1.0.0: The
valuekeyword-argument was converted from astrtoconstants.Justify.
- ThermalPrinter.left_blank(value: int = 0) None ΒΆ
Set the left margin, in points.
- Parameters:
value (int ) β Value to pass to the printer (min=0, max=255).
- Raises:
ThermalPrinterValueError β On incorrect
valueβs type, or value.
Added in version 1.0.0.
- ThermalPrinter.left_margin(margin: int = 0) None ΒΆ
Set the left margin, in 8-points.
- Parameters:
margin (int ) β The new margin (min=0, max=47).
- Raises:
ThermalPrinterValueError β On incorrect
marginβs type, or value.
- ThermalPrinter.line_spacing(spacing: int = 30) None ΒΆ
Set the line spacing.
- Parameters:
spacing (int ) β The new spacing (min=0, max=255).
- Raises:
ThermalPrinterValueError β On incorrect
spacingβs type, or value.
- ThermalPrinter.rotate(state: bool = False) None ΒΆ
Turn on/off 90Β° clockwise rotation.
- Parameters:
state (bool ) β Enabled if
stateisTrue, else disabled.
- ThermalPrinter.size(value: Size = Size.SMALL) None ΒΆ
Set the text size.
- Parameters:
value (Size) β The size to use.
Note
This method affects
max_column.Changed in version 1.0.0: The
valuekeyword-argument was converted from astrtoconstants.Size.
- ThermalPrinter.strike(state: bool = False) None ΒΆ
Turn on/off the double-strike mode.
- Parameters:
state (bool ) β Enabled if
stateisTrue, else disabled.
Encoding and CharsetsΒΆ
ChineseΒΆ
Printer StateΒΆ
- ThermalPrinter.offline() None ΒΆ
Take the printer offline. Upcoming print commands issued will be ignored until
online()is called.
- ThermalPrinter.sleep(seconds: int = 1) None ΒΆ
Put the printer into a low-energy state.
- Parameters:
seconds (int ) β Value to pass to the printer (min=0).
- Raises:
ThermalPrinterValueError β On incorrect
secondsβs type, or value.
- ThermalPrinter.status(*, raise_on_error: bool = True) dict [str , bool ]ΒΆ
Return the printer status.
- Parameters:
raise_on_error (bool ) β Raise on error.
- Raises:
ThermalPrinterCommunicationError β If the RX pin is not connected, and if
raise_on_errorisTrue.- Return type:
- Returns:
See
status_to_dict().
Added in version 0.2.0: The
raise_on_errorkeyword-argument.Removed in version 1.0.0: The
movementkey as it would always beFalse.
Special MethodsΒΆ
- ThermalPrinter.flush(clear: bool = False) None ΒΆ
Remove the print data from the output buffer.
- Parameters:
clear (bool ) β Set to
Trueto also clear the input buffer.
- ThermalPrinter.init(heat_time: int ) None ΒΆ
Set printer heat properties.
- Parameters:
heat_time (int ) β Printer heat time.
Added in version 1.0.0.
- ThermalPrinter.print_char(char: str ) None ΒΆ
Test one character with all supported code pages.
- Parameters:
char (str ) β The character to print.
Say you are looking for the good code page to print a sequence, you can print it using every code pages:
>>> printer.print_char("η°")Added in version 1.0.0.
AttributesΒΆ
All these attributes are read-only.
ConstantsΒΆ
Barcode TypesΒΆ
- enum thermalprinter.constants.BarCode(value)ΒΆ
Barcode types.
Valid values are as follows:
- UPC_A = <BarCode.UPC_A: 65, range: 11 <= len(data) <= 12>ΒΆ
- UPC_E = <BarCode.UPC_E: 66, range: 11 <= len(data) <= 12>ΒΆ
- JAN13 = <BarCode.JAN13: 67, range: 12 <= len(data) <= 13>ΒΆ
- JAN8 = <BarCode.JAN8: 68, range: 7 <= len(data) <= 8>ΒΆ
- CODE39 = <BarCode.CODE39: 69, range: 1 <= len(data) <= 255>ΒΆ
- ITF = <BarCode.ITF: 70, range: 1 <= len(data) <= 255>ΒΆ
- CODABAR = <BarCode.CODABAR: 71, range: 1 <= len(data) <= 255>ΒΆ
- CODE93 = <BarCode.CODE93: 72, range: 1 <= len(data) <= 255>ΒΆ
- CODE128 = <BarCode.CODE128: 73, range: 2 <= len(data) <= 255>ΒΆ
Barcode PositionsΒΆ
Characters SetsΒΆ
- enum thermalprinter.constants.CharSet(value)ΒΆ
Character sets.
Valid values are as follows:
- USA = <CharSet.USA: 0>ΒΆ
- FRANCE = <CharSet.FRANCE: 1>ΒΆ
- GERMANY = <CharSet.GERMANY: 2>ΒΆ
- UK = <CharSet.UK: 3>ΒΆ
- DENMARK = <CharSet.DENMARK: 4>ΒΆ
- SWEDEN = <CharSet.SWEDEN: 5>ΒΆ
- ITALY = <CharSet.ITALY: 6>ΒΆ
- SPAIN = <CharSet.SPAIN: 7>ΒΆ
- JAPAN = <CharSet.JAPAN: 8>ΒΆ
- NORWAY = <CharSet.NORWAY: 9>ΒΆ
- DENMARK2 = <CharSet.DENMARK2: 10>ΒΆ
- SPAIN2 = <CharSet.SPAIN2: 11>ΒΆ
- LATIN_AMERICAN = <CharSet.LATIN_AMERICAN: 12>ΒΆ
- KOREA = <CharSet.KOREA: 13>ΒΆ
- SLOVENIA = <CharSet.SLOVENIA: 14>ΒΆ
- CHINA = <CharSet.CHINA: 15>ΒΆ
Chinese FormatsΒΆ
Code PagesΒΆ
- enum thermalprinter.constants.CodePage(value)ΒΆ
Character code tables.
Valid values are as follows:
- CP437 = <CodePage.CP437: (0, 'the United States of America, European standard')>ΒΆ
- CP932 = <CodePage.CP932: (1, 'Katakana')>ΒΆ
- CP850 = <CodePage.CP850: (2, 'Multi language')>ΒΆ
- CP860 = <CodePage.CP860: (3, 'Portuguese')>ΒΆ
- CP863 = <CodePage.CP863: (4, 'Canada, French')>ΒΆ
- CP865 = <CodePage.CP865: (5, 'Western Europe')>ΒΆ
- CYRILLIC = <CodePage.CYRILLIC: (6, 'The Slavic language')>ΒΆ
- CP866 = <CodePage.CP866: (7, 'The Slavic 2')>ΒΆ
- MIK = <CodePage.MIK: (8, 'The Slavic / Bulgaria')>ΒΆ
- CP755 = <CodePage.CP755: (9, 'Eastern Europe, Latvia 2')>ΒΆ
- IRAN = <CodePage.IRAN: (10, 'Iran, Persia')>ΒΆ
- CP862 = <CodePage.CP862: (15, 'Hebrew')>ΒΆ
- CP1252 = <CodePage.CP1252: (16, 'Latin 1 [WCP1252]')>ΒΆ
- CP1253 = <CodePage.CP1253: (17, 'Greece [WCP1253]')>ΒΆ
- CP852 = <CodePage.CP852: (18, 'Latina 2')>ΒΆ
- CP858 = <CodePage.CP858: (19, 'A variety of language Latin 1 + Europe')>ΒΆ
- IRAN2 = <CodePage.IRAN2: (20, 'Persian')>ΒΆ
- LATVIA = <CodePage.LATVIA: (21, '')>ΒΆ
- CP864 = <CodePage.CP864: (22, 'Arabic')>ΒΆ
- ISO_8859_1 = <CodePage.ISO_8859_1: (23, 'Western Europe')>ΒΆ
- CP737 = <CodePage.CP737: (24, 'Greece')>ΒΆ
- CP1257 = <CodePage.CP1257: (25, 'The Baltic Sea')>ΒΆ
- THAI = <CodePage.THAI: (26, 'Thai Wen')>ΒΆ
- CP720 = <CodePage.CP720: (27, 'Arabic')>ΒΆ
- CP855 = <CodePage.CP855: (28, '')>ΒΆ
- CP857 = <CodePage.CP857: (29, 'Turkish')>ΒΆ
- CP1250 = <CodePage.CP1250: (30, 'Central Europe [WCP1250]')>ΒΆ
- CP775 = <CodePage.CP775: (31, '')>ΒΆ
- CP1254 = <CodePage.CP1254: (32, 'Turkish [WCP1254]')>ΒΆ
- CP1255 = <CodePage.CP1255: (33, 'Hebrew [WCP1255]')>ΒΆ
- CP1256 = <CodePage.CP1256: (34, 'Arabic [WCP1256]')>ΒΆ
- CP1258 = <CodePage.CP1258: (35, 'Vietnamese [WCP1258]')>ΒΆ
- ISO_8859_2 = <CodePage.ISO_8859_2: (36, 'Latin 2')>ΒΆ
- ISO_8859_3 = <CodePage.ISO_8859_3: (37, 'Latin 3')>ΒΆ
- ISO_8859_4 = <CodePage.ISO_8859_4: (38, 'Baltic languages')>ΒΆ
- ISO_8859_5 = <CodePage.ISO_8859_5: (39, 'The Slavic language')>ΒΆ
- ISO_8859_6 = <CodePage.ISO_8859_6: (40, 'Arabic')>ΒΆ
- ISO_8859_7 = <CodePage.ISO_8859_7: (41, 'Greece')>ΒΆ
- ISO_8859_8 = <CodePage.ISO_8859_8: (42, 'Hebrew')>ΒΆ
- ISO_8859_9 = <CodePage.ISO_8859_9: (43, 'Turkish')>ΒΆ
- ISO_8859_15 = <CodePage.ISO_8859_15: (44, 'Latin 9')>ΒΆ
- THAI2 = <CodePage.THAI2: (45, 'Thai Wen 2')>ΒΆ
- CP856 = <CodePage.CP856: (46, '')>ΒΆ
- CP874 = <CodePage.CP874: (47, '')>ΒΆ
Code Pages FallbackΒΆ
- enum thermalprinter.constants.CodePageConverted(value)ΒΆ
Some code pages are not available on Python, so we use a little translation table.
Valid values are as follows:
- MIK = <CodePageConverted.MIK: 'ISO-8859-5'>ΒΆ
- CP755 = <CodePageConverted.CP755: 'UTF-8'>ΒΆ
- IRAN = <CodePageConverted.IRAN: 'CP1256'>ΒΆ
- LATVIA = <CodePageConverted.LATVIA: 'ISO-8859-4'>ΒΆ
- THAI = <CodePageConverted.THAI: 'ISO-8859-11'>ΒΆ
Note
If you find a better fit for one of the code page below, open an issue please (or better: send a patch ) π€
CommandsΒΆ
Defaults ParametersΒΆ
- enum thermalprinter.constants.Defaults(value)ΒΆ
Default printer parameters.
Hint
Most of them can be tweaked by special environment variables following the given syntax:
TP_XXX. Example:export TP_BAUDRATE=9600will setDefaults.BAUDRATEto9600.Valid values are as follows:
- BARCODE_HEIGHT = <Defaults.BARCODE_HEIGHT: 162>ΒΆ
- BARCODE_WIDTH = <Defaults.BARCODE_WIDTH: 3>ΒΆ
- BAUDRATE = <Defaults.BAUDRATE: 19200>ΒΆ
- BYTE_TIME = <Defaults.BYTE_TIME: 5e-05>ΒΆ
- DOT_FEED_TIME = <Defaults.DOT_FEED_TIME: 0.0021>ΒΆ
- DOT_PRINT_TIME = <Defaults.DOT_PRINT_TIME: 0.03>ΒΆ
- HEAT_INTERVAL = <Defaults.HEAT_INTERVAL: 2>ΒΆ
- HEAT_TIME = <Defaults.HEAT_TIME: 80>ΒΆ
- LINE_SPACING = <Defaults.LINE_SPACING: 30>ΒΆ
- MOST_HEATED_POINT = <Defaults.MOST_HEATED_POINT: 9>ΒΆ
- PORT = <Defaults.PORT: '/dev/ttyAMA0'>ΒΆ
- READ_TIMEOUT = <Defaults.READ_TIMEOUT: 0.0>ΒΆ
- WRITE_TIMEOUT = <Defaults.WRITE_TIMEOUT: 1.0>ΒΆ
Text JustificationΒΆ
Text SizeΒΆ
Text UnderlineΒΆ
OtherΒΆ
- thermalprinter.constants.MAX_IMAGE_WIDTH = 384ΒΆ
Max image width.
ExceptionsΒΆ
- exception thermalprinter.exceptions.ThermalPrinterErrorΒΆ
Base class for thermal printer exceptions.
- exception thermalprinter.exceptions.ThermalPrinterCommunicationErrorΒΆ
Raised on communication error with the printer.
- exception thermalprinter.exceptions.ThermalPrinterValueErrorΒΆ
Raised on incorrect type, or value, passed to any method.