用于escpos打印机的python接口

python-printer-escpos的Python项目详细描述


python打印机escpos

https://img.shields.io/badge/status-production-blue.svghttps://img.shields.io/badge/python%20version-2.7-blue.svghttps://img.shields.io/github/license/mashape/apistatus.svg

文档

如果您刚开始使用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')

对齐打印输出

为文本设置文本对齐方式。

printer.align('center')printer.text('This text is center aligned')printer.align('right')printer.text('This text is right aligned')

更改字符间距

设置打印输出中每个字母之间的间距。

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.horizontalPosition(100)printer.text('This text starts at 1/6inches from left margin')

剪纸

使用切纸机切纸(仅适用于带切纸机的型号)

printer.cutPaper()

打开现金抽屉

在闭合接触界面上发送电脉冲。通常与打开抽屉的现金抽屉相连。

printer.drawerKickPulse()

要首先运行命令,必须按照上一节中的说明创建打印机对象

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
用户界面java,使用gui连接到另一台计算机/服务器的文件系统   运行sbt的ubuntu返回错误:“javahome需要<path>参数”   java如何在Android中处理许多ImageView而不出现内存问题?   查询中非法字符的java相同URL失败   安卓取消引用可能会产生“java”。lang.NullPointerException'   java中的indexoutofboundsexception“java.lang.ArrayIndexOutOfBoundsException”错误   xml Java将dom保存到文件>文件在程序结束后由另一个进程打开   Java的垃圾收集器是如何工作的?   Java如何筛选值(列表)   java处理字符串我怎样才能像在真实的书籍中一样在上面部分生成“小数字”呢?   java SonarQube是否有一个API来获取所有项目分析的一部分?   java startActivity(intent)什么都不做   JAVAutil。扫描器类Java   java如何从Firebase更新电子邮件?UpdateMail方法已被弃用   java Hibernate。如何正确组织带有注释的onetomany关系?   在java中获得卷标和驱动器号之间的映射(而不是FileSystemView)的解决方法是什么   java查找文件的路径