escpos.commandset package

Submodules

escpos.commandset.generic module

Generic ESCPOS command set for escpos module. You can override Generic command set by writing a module specific to your printer model in the escpos.commandset.* namespace as escpos.commandset.examplecommands and putting a class named ExampleCommands in that package and define the special commands in methods of ExampleCommands class. Then when initializing your printer pass commandSet='ExampleCommands' to getXXXPrinter function. For example to use Generic commandset on a USB printer:

printer = new getUSBPrinter(commandSet='Generic')(idVendor=0x1504, idProduct=0x0006)
class escpos.commandset.generic.Generic[source]

This is a generic(and default) commandset that works for most of the ESCPOS compliant printers.

align(align='left')[source]

Align printing to left, center, right or full justification. Some printers require a line feed before justification change for it to have effect. Always set justification at the beginning of a line.

Parameters:align (str) – Possible values ‘left’, ‘center’, ‘right’ or ‘full’ (default: ‘left’)

Example:

printer.lf()
printer.align('center')
printer.text('This text is center aligned')
printer.lf()
printer.align('right')
printer.text('This text is right aligned')
printer.lf()
printer.align('left')
printer.text('This text is left aligned')
printer.lf()
printer.align('full')
printer.text('This text is full justified') # Only available in select models
barcode(text='shantanu', textPosition='below', font='b', height=50, width=2, system='CODE93')[source]

Prints barcode to the Printer

Parameters:
  • text (str) – Text to be printed as barcode
  • textPosition (str) – Position of Human readable text for the barcode - ‘none’, ‘above’, ‘below’, ‘aboveandbelow’
  • font (str) – font for the Human readable text - ‘a’ or ‘b’
  • height (int) – Barcode height no of dots in vertical direction
  • width (int) – Width of barcode - 2 => 0.25mm, 3 => 0.375mm, 4 => 0.5mm, 5 => 0.625mm, 6 => 0.75mm
  • system (int) – Barcode system - ‘UPC-A’, ‘UPC-E’, ‘JAN13’, ‘JAN8’, ‘CODE39’, ‘ITF’, ‘CODABAR’, ‘CODE93’, ‘CODE128’

Example:

printer.barcode(text='Shantanu', textPosition='below', font='b', height=100, width=2, system='CODE93')
beep()[source]

Make the printer make a beep, this is supported on almost all printers with beepers

Example:

printer.beep()
bold(bold=True)[source]

Makes text emphasized or unemphasized (bold=False)

Parameters:bold (bool) – True for turning on bold font, False for switching to normal font weight (default: True)

Example:

printer.bold()
printer.text('This text is bold text')
printer.lf()
printer.bold(False)
printer.text('This text is not bold')
printer.lf()
charSpacing(charSpacing=0)[source]

Set right-side character spacing

Parameters:charSpacing (int) – Char spacing, range from 1 to 256 (default: 0)

Example:

printer.charSpacing(1)
printer.text('This text has normal right char spacing')
printer.lf()
printer.charSpacing(5)
printer.text('This text has 5 right char spacing')
printer.lf()
color(color=0)[source]

Most thermal printers support just one color, black. Some ESCPOS printers(especially dot matrix) also support a second color(usually red). A few rarer models also support upto 7 different colors. Do note that in many printer models, this command only works when the color is set at the beginning of a new line before any text is printed.

Parameters:color (int) – 0 for switching to primary color, a positive integer for switching to a secondary color (default: 0)

Example:

printer.color()
printer.text('This text is in primary color')
printer.lf()
printer.color(1)
printer.text('This text is not in color 2')
printer.lf()
printer.color(3)
printer.text('This text is not in color 3')
printer.lf()
cr()[source]

Print and carriage return. When automatic line feed is enabled this method works the same as lf , else it is ignored.

Example:

printer.cr()
cutPaper(cut='partial', feed=True)[source]

Cuts the paper in escpos printers that support this function

Parameters:
  • cut (str) – ‘partial’ or ‘full’ cut (default: ‘partial’)
  • feed (bool) – True for feeding paper to cutting position before executing cut, false for cutting paper at current position (default: True)

Example:

printer.cutPaper()
disable()[source]

Disable the printer. After a disable() command the printer ignores all commands except enable() or other real-time commands.

Example:

printer.disable()
printer.ensable()
doubleHeight(doubleHeight=True)[source]

Makes text double height, its recommended to use textSize() for supported printers

Parameters:doubleHeight (bool) – True for turning on double height text, False for switching to normal height text (default: True)

Example:

printer.doubleHeight()
printer.text('This text is double height text')
printer.lf()
printer.doubleHeight(False)
printer.text('This text is not double height')
printer.lf()
doubleStrike(doubleStrike=True)[source]

Puts a double strike on the text

Parameters:doubleStrike (bool) – True for turning on font double strike, False for switching off font double strike (default: True)

Example:

printer.doubleStrike()
printer.text('This text is double strike text')
printer.lf()
printer.doubleStrike(False)
printer.text('This text is not double strike')
printer.lf()
doubleWidth(doubleWidth=True)[source]

Makes text double width, its recommended to use textSize() for supported printers

Parameters:doubleWidth (bool) – True for turning on double width text, False for switching to normal width text (default: True)

Example:

printer.doubleWidth()
printer.text('This text is double width text')
printer.lf()
printer.doubleWidth(False)
printer.text('This text is not double width')
printer.lf()
drawerKickPulse(pin=0, time=8)[source]

Generate pulse in real-time on one of the connectors, this is often connected to the cash drawer attached to cashier terminals.

Parameters:
  • pin (int) – Pin to which the pulse must be sent this must be either 0 or 1 for pins 2 or 5 respectively (default: 0)
  • time (int) – Duration of the pulse in units of 100 ms (range: 1 - 8) (default: 8)

Example:

printer.drawerKickPulse()
enable()[source]

Enable the printer after a disable() command

Example:

printer.disable()
printer.enable()
font(font='a')[source]

Set printer font. Most printers support two fonts i.e. ‘a’ or ‘b’, some may support a third font ‘c’

Parameters:font (str) – Font for the printer, default ‘a’. The font may be either ‘a’, ‘b’ or ‘c’ (default:’a’)

Example:

printer.font('b')
printer.text('This text is in font b')
printer.lf()
printer.font('a')
printer.text('This text is in font a')
printer.lf()
horizontalPosition(horizontalPosition=0)[source]

Moves the horizontal print position relative to the left margin in 1/60th of inches. The printer ignores this command if the specified position is to the right of the right margin.

Parameters:horizontalPosition (int) – horizontal position from left margin in 1/60th of inches (default: 0)

Example:

printer.lf()
printer.horizontalPosition(100)
printer.text('This text starts at 1/6inches from left margin')
image(path)[source]

Print a image from a file

Parameters:path (str) – Path to the image file to be printed

Example:

printer.image('/home/shantanu/companylogo.gif')
initialize()[source]

Initializes the Printer. Clears the data in print buffer and resets the printer to the mode that was in effect when the power was turned on. This function is automatically called on creation of printer object unless specifically disabled.

  • Any macro definitions are not cleared.
  • Offline response selection is not cleared.
  • Contents of user NV memory are not cleared.
  • NV graphics (NV bit image) and NV user memory are not cleared.
  • The maintenance counter value is not affected by this command.
  • The specifying of offline response isn’t cleared.

Example:

printer.initialize()
invert(invert=True)[source]

Invert text colors printing, switches from black on white background to white on black background

Parameters:invert (bool) – True for turning on inverted colors, False for switching off inverted colors (default: True)

Example:

printer.invert()
printer.text('This text is in inverted colors')
printer.lf()
printer.invert(False)
printer.text('This text is not in inverted colors')
printer.lf()
leftMargin(leftMargin=10)[source]

Sets the left margin for printing. Set the left margin at the beginning of a line. The printer ignores any data preceding this command on the same line in the buffer.

In page mode sets the left margin to leftMargin x (horizontal motion unit) from the left edge of the printable area

Parameters:leftMargin (int) – Left Margin, range: 0 to 65535. If the margin exceeds the printable area, the left margin is automatically set to the maximum value of the printable area.

Example:

printer.lf()
printer.leftMargin(30)
printer.text('This text has left Margin of 30')
lf()[source]

Line feed. Moves to the next line. You can substitute this method with printer.text(‘n’)

Example:

printer.lf()
lineSpacing(lineSpacing=30, commandSet='3')[source]

Set line character spacing, note that some printers may not support all commandsets for setting a line spacing. The most commonly available commandSet(‘3’) is chosen by default.

Parameters:
  • lineSpacing (int) – Line spacing, range from 0 to 255 when commandSet is ‘+’ or ‘3’, sets line spacing to lineSpacing/360 of an inch if commandSet is ‘+’, lineSpacing/180 of an inch if commandSet is ‘3’ and lineSpacing/60 of an inch if commandSet is ‘A’ (default: 30)
  • commandSet (str) – ESCPOS provides three aternate commands for setting line spacing i.e. ‘+’, ‘3’, ‘A’ (default : ‘3’).
  1. When commandSet is ‘+’ lineSpacing is set to lineSpacing/360 of an inch, 0 <= lineSpacing <= 255
  2. when commandSet is ‘3’ lineSpacing is set to lineSpacing/180 of an inch, 0 <= lineSpacing <= 255
  3. when commandSet is ‘A’ lineSpacing is set to lineSpacing/60 of an inch, 0 <= lineSpacing <= 85

Example:

printer.lineSpacing()
printer.text('This text has 1/6 inch line spacing')
printer.lf()
printer.lineSpacing(5)
printer.text('This text has 5/60 inch line spacing')
printer.lf()
printAreaWidth(width=65535)[source]

Set Print area width for the thermal printer, In Standard mode, sets the print area width to

width x basic calculated pitch
Parameters:width (int) – 16 bits value range, i.e. int between 0 to 65535 specifying print area width in basic calculated pitch

This command is effective only when processed at the beginning of the line when standard mode is being used. Printable area width setting is effective until initialize is executed, the printer is reset, or the power is turned off.

Example:

printer.lf()
printer.printAreaWidth(200)
printer.text('Set print area width to 200')
printer.lf()
printer.text('1234567890123456789012345678901234567890123456789012345678901234567890')
printer.printAreaWidth()
qr(text)[source]

Print QR Code for the provided string

Parameters:text (str) – Text to be printed to the QR code

Example:

printer.qr('My name is Shantanu Bhadoria')
printer.qr('WIFI:T:WPA;S:ShantanusWifi;P:wifipasswordhere;;')  # Create a QR code for connecting to a Wifi
rotate90(rotate=True)[source]

Rotates printing by 90 degrees

Parameters:rotate (bool) – Rotate by 90 degrees if True, else set to normal style (default: True)

Example:

printer.lf()
printer.rotate90(100)
printer.text('This text is rotated 90 degrees')
tab()[source]

Moves the cursor to next horizontal tab position like a ‘t’. This command is ignored unless the next horizontal tab position has been set. You may substitute this command with a printer.text(‘t’) as well. See tabPositions() to understand the best way to use tabs to print out beautiul receipts.

Example:

printer.tab()
tabPositions(positions=[8, 16, 24, 32, 40])[source]

Set tab positions for printer. Each tab() will move the cursor to the next tab position. This is useful when printing receipts and bills as this allows you to set one tab position for the prices. so after printing the plu name, add a tab to immediately move to the tab position to print the price. e.g.

Parameters:positions (list) – A list of int(s) specifying tab positions e.g. [8,16,24,32,40]

Example:

printer.tabPositions([3, 32])
for plu in plus:
    printer.text(plu.quantity)
    printer.tab()
    printer.text(' x ' + plu.name)
    printer.tab()
    printer.text('$' + plu.price)

This would print a well aligned receipt like so:

10 x Guiness Beer              $24.00
2  x Pizza                     $500.50
1  x Tandoori Chicken          $50.20
textSize(height=1, width=1)[source]

Sets font text Size. Note that many printers will not support the full range of text heights and widths, e.g. many models may only support a maximum height and width of 8

Parameters:
  • height (int) – Text height, range from 1 to 16 (default: 0)
  • width (int) – Text width, range from 1 to 16 (default: 0)

Example:

printer.textSize(2,3)
printer.text('This text is double height and thrice the width')
printer.lf()
printer.textSize(1,4)
printer.text('This text is normal height and quadruple width')
printer.lf()
underline(underline=True, doubleDot=False)[source]

Puts a underline under the text

Parameters:
  • underline (bool) – True for turning on font underline, False for switching off font underline (default: True)
  • doubleDot (bool) – True for double dot width underline, False for single dot width underline (default : False).

Example:

printer.underline()
printer.text('This text is underlined text')
printer.lf()
printer.underline(True,True)
printer.text('This text is double dot width underlined text')
printer.lf()
printer.underline(False)
printer.text('This text is not underlined')
printer.lf()
upsideDown(upsideDown=True)[source]

Upside down text printing

Parameters:upsideDown (bool) – True for turning on upside down printing, False for switching off upside down printing (default: True)

Example:

printer.upsideDown()
printer.text('This text is upside down')
printer.lf()
printer.upsideDown(False)
printer.text('This text is not upside down')
printer.lf()

Module contents