用于escpos打印机的python接口
python-printer-escpos的Python项目详细描述
python打印机escpos
文档
如果您刚开始使用pos收据打印机,则必须首先参考http://pythonhosted.org/python-printer-escpos/才能开始使用。
python打印机escpos提供四种不同类型的打印机连接来与escpos打印机通信。
安装
首先获得一些图像打印等的先决条件。
pip install Pillow
安装python打印机escpos
pip install python-printer-escpos
创建打印机对象
USB打印机
如果打印机通过USB接口连接,请使用此连接模式
fromescpos.connectionsimportgetUSBPrinterprinter=getUSBPrinter()(idVendor=0x1504,idProduct=0x0006inputEndPoint=0x82,outputEndPoint=0x01)# Create the printer object with the connection paramsprinter.text("Hello World")printer.lf()
网络打印机
如果打印机在网络上,请使用此连接模式。9100是收据打印机最常用的端口号。但是,您必须检查打印机手册以获得正确的打印机端口号。
fromescpos.connectionsimportgetNetworkPrinterprinter=getNetworkPrinter()(host='192.168.0.20',port=9100)printer.text("Hello World")printer.lf()
串行打印机
打印机也可以通过rs232串行接口连接。对于串行连接,您必须获得正确的打印机波特率。
fromescpos.connectionsimportgetSerialPrinterprinter=getSerialPrinter()(dev='/dev/ttyS0',baudrate=9600)printer.text("Hello World")printer.lf()
设备文件打印机
如果无法确定如何使用其他连接模式,可以尝试直接打印到打印机设备文件。这在大多数用例中都有效,但是在此模式下您将无法使用“读取设备状态”功能。
fromescpos.connectionsimportgetFilePrinterprinter=getFilePrinter()(dev='/dev/ttys2')printer.text("Hello World")printer.lf()
示例
打印hello world
printer.text("Hello World")printer.lf()
打印图像
将图像打印到打印机(最大宽度:512px,最大高度:255px)
printer.image('/home/shantanu/companylogo.gif')
打印二维码
打印二维码到打印机。该示例还包括将wifi二维码打印到打印机的方法。
printer.qr('My name is Shantanu Bhadoria')printer.qr('WIFI:T:WPA;S:ShantanusWifi;P:wifipasswordhere;;')# Create a QR code for connecting to a Wifiprinter.lf()
打印条形码
将可用格式之一的条形码打印到打印机。您也可以在条形码上方或下方指定hri文本位置。
printer.barcode(text='Shantanu',textPosition='below',font='b',height=100,width=2,system='CODE93')printer.lf()
对齐打印输出
为文本设置文本对齐方式。
printer.align('center')printer.text('This text is center aligned')printer.align('right')printer.text('This text is right aligned')
以粗体打印
以粗体打印文本
printer.bold()printer.text('This text is bold text')printer.lf()printer.bold(False)printer.text('This text is not bold')printer.lf()
更改字符间距
设置打印输出中每个字母之间的间距。
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()
更改打印颜色(仅适用于某些型号)
此功能在某些打印机中受支持,特别是带有两个色带的点阵收据打印机。
printer.color()printer.text('This text is in primary color')printer.lf()printer.color(1)printer.text('This text is in color 1')printer.lf()
更改字体高度
设置字体高度。
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()
将字体改为双倍宽度
设置字体宽度。
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()
切换字体
在打印机字体之间切换。支持有限的字体,大多数仅为“A”或“B”
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()
反转文本(黑白到黑白)
反转打印,使其在黑色背景上以白色打印。
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()
设置左边距
设置左边距。
printer.leftMargin(30)printer.text('This text has left Margin of 30')printer.lf()
设置行距
设置行间距。
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()
设置打印区域宽度
设置可打印区域的宽度。
printer.printAreaWidth(200)printer.text('Set print area width to 200')printer.lf()printer.text('1234567890123456789012345678901234567890123456789012345678901234567890')printer.printAreaWidth()printer.lf()
将文本旋转90度
将所有文本旋转90度
printer.rotate90(100)printer.text('This text is rotated 90 degrees')
设置制表位
设置制表位。这在与tab()结合使用时非常有用,它可以打印出带有plus、quantity和price的收据。
printer.tabPositions([3,32])forpluinplus:printer.text(plu.quantity)printer.tab()printer.text(' x '+plu.name)printer.tab()printer.text('$'+plu.price)""" 2 x Guinness $12.00 3 x Margharita Pizza $45.00 10 x Coke $25.00 2 x Tandoori Chicken $40.00 """
下划线
给文本加下划线。
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()
打印颠倒
以倒置模式打印文本。
printer.upsideDown()printer.text('This text is upside down')printer.lf()printer.upsideDown(False)printer.text('This text is not upside down')printer.lf()
设置水平位置
设置水平打印位置。
printer.horizontalPosition(100)printer.text('This text starts at 1/6inches from left margin')
剪纸
使用切纸机切纸(仅适用于带切纸机的型号)
printer.cutPaper()
打开现金抽屉
在闭合接触界面上发送电脉冲。通常与打开抽屉的现金抽屉相连。
printer.drawerKickPulse()
要首先运行命令,必须按照上一节中的说明创建打印机对象