Printer implementations

Module escpos.printer

printer implementations.

class escpos.printer.Usb(idVendor=None, idProduct=None, usb_args={}, timeout=0, in_ep=130, out_ep=1, *args, **kwargs)[source]

Bases: Escpos

USB printer.

This class describes a printer that natively speaks USB.

inheritance:

Inheritance diagram of escpos.printer.Usb
static is_usable()[source]

Indicate whether this printer class is usable.

Will return True if dependencies are available. Will return False if not.

Return type:

bool

open(raise_not_found=True)[source]

Search device on USB tree and set it as escpos device.

By default raise an exception if device is not found.

Parameters:

raise_not_found (bool) – Default True. False to log error but do not raise exception.

Raises:

DeviceNotFoundError

Raises:

USBNotFoundError

Return type:

None

close()[source]

Release USB interface.

Return type:

None

class escpos.printer.File(devfile='', auto_flush=True, *args, **kwargs)[source]

Bases: Escpos

Generic file printer.

This class is used for parallel port printer or other printers that are directly attached to the filesystem. Note that you should stay away from using USB-to-Parallel-Adapter since they are unreliable and produce arbitrary errors.

inheritance:

Inheritance diagram of escpos.printer.File
static is_usable()[source]

Indicate whether this printer class is usable.

Will return True if dependencies are available. Will return False if not.

Return type:

bool

open(raise_not_found=True)[source]

Open system file.

By default raise an exception if device is not found.

Parameters:

raise_not_found (bool) – Default True. False to log error but do not raise exception.

Raises:

DeviceNotFoundError

Return type:

None

flush()[source]

Flush printing content.

Return type:

None

close()[source]

Close system file.

Return type:

None

class escpos.printer.Network(host='', port=9100, timeout=60, *args, **kwargs)[source]

Bases: Escpos

Network printer.

This class is used to attach to a networked printer. You can also use this in order to attach to a printer that is forwarded with socat.

If you have a local printer on parallel port /dev/usb/lp0 then you could start socat with:

socat -u TCP4-LISTEN:4242,reuseaddr,fork OPEN:/dev/usb/lp0

Then you should be able to attach to port 4242 with this class. Otherwise the normal use case would be to have a printer with Ethernet interface. This type of printer should work the same with this class. For the address of the printer check its manuals.

inheritance:

Inheritance diagram of escpos.printer.Network
static is_usable()[source]

Indicate whether this printer class is usable.

Will return True if dependencies are available. Will return False if not.

Return type:

bool

open(raise_not_found=True)[source]

Open TCP socket with socket-library and set it as escpos device.

By default raise an exception if device is not found.

Parameters:

raise_not_found (bool) – Default True. False to log error but do not raise exception.

Raises:

DeviceNotFoundError

Return type:

None

close()[source]

Close TCP connection.

Return type:

None

class escpos.printer.Serial(devfile='', baudrate=9600, bytesize=8, timeout=1, parity=None, stopbits=None, xonxoff=False, dsrdtr=True, *args, **kwargs)[source]

Bases: Escpos

Serial printer.

This class describes a printer that is connected by serial interface.

inheritance:

Inheritance diagram of escpos.printer.Serial
static is_usable()[source]

Indicate whether this printer class is usable.

Will return True if dependencies are available. Will return False if not.

Return type:

bool

open(raise_not_found=True)[source]

Set up serial port and set is as escpos device.

By default raise an exception if device is not found.

Parameters:

raise_not_found (bool) – Default True. False to log error but do not raise exception.

Raises:

DeviceNotFoundError

Return type:

None

close()[source]

Close Serial interface.

Return type:

None

class escpos.printer.LP(printer_name='', *args, **kwargs)[source]

Bases: Escpos

Simple UNIX lp command raw printing.

Thanks to Oyami-Srk comment.

inheritance:

Inheritance diagram of escpos.printer.LP
static is_usable()[source]

Indicate whether this printer class is usable.

Will return True if dependencies are available. Will return False if not.

Return type:

bool

property printers: dict

Available CUPS printers.

open(job_name='python-escpos', raise_not_found=True, _close_opened=True)[source]

Invoke _lp_ in a new subprocess and wait for commands.

By default raise an exception if device is not found.

Parameters:

raise_not_found (bool) – Default True. False to log error but do not raise exception.

Raises:

DeviceNotFoundError

Return type:

None

close()[source]

Stop the subprocess.

Return type:

None

flush()[source]

End line and wait for new commands.

Return type:

None

class escpos.printer.Dummy(*args, **kwargs)[source]

Bases: Escpos

Dummy printer.

This class is used for saving commands to a variable, for use in situations where there is no need to send commands to an actual printer. This includes generating print jobs for later use, or testing output.

inheritance:

Inheritance diagram of escpos.printer.Dummy
static is_usable()[source]

Indicate whether this printer class is usable.

Will return True if dependencies are available. Will return False if not.

Return type:

bool

property output: bytes

Get the data that was sent to this printer.

clear()[source]

Clear the buffer of the printer.

This method can be called if you send the contents to a physical printer and want to use the Dummy printer for new output.

Return type:

None

close()[source]

Close not implemented for Dummy printer.

Return type:

None

class escpos.printer.CupsPrinter(printer_name='', *args, **kwargs)[source]

Bases: Escpos

Simple CUPS printer connector.

Note

Requires pycups which in turn needs the cups development library package:
  • Ubuntu/Debian: libcups2-dev

  • OpenSuse/Fedora: cups-devel

inheritance:

Inheritance diagram of escpos.printer.CupsPrinter
static is_usable()[source]

Indicate whether this printer class is usable.

Will return True if dependencies are available. Will return False if not.

Return type:

bool

property printers: dict

Available CUPS printers.

open(job_name='python-escpos', raise_not_found=True)[source]

Set up a new print job and target the printer.

A call to this method is required to send new jobs to the CUPS connection after close.

Defaults to default CUPS printer. Creates a new temporary file buffer.

By default raise an exception if device is not found.

Parameters:

raise_not_found (bool) – Default True. False to log error but do not raise exception.

Raises:

DeviceNotFoundError

Return type:

None

send()[source]

Send the print job to the printer.

Return type:

None

close()[source]

Close CUPS connection.

Send pending job to the printer if needed.

Return type:

None

class escpos.printer.Win32Raw(printer_name='', *args, **kwargs)[source]

Bases: Escpos

Printer binding for win32 API.

Uses the module pywin32 for printing.

inheritance:

Inheritance diagram of escpos.printer.Win32Raw
static is_usable()[source]

Indicate whether this printer class is usable.

Will return True if dependencies are available. Will return False if not.

Return type:

bool

property printers: dict

Available Windows printers.

open(job_name='python-escpos', raise_not_found=True)[source]

Open connection to default printer.

By default raise an exception if device is not found.

Parameters:

raise_not_found (bool) – Default True. False to log error but do not raise exception.

Raises:

DeviceNotFoundError

Return type:

None

close()[source]

Close connection to default printer.

Return type:

None