You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python热敏收据打印求助:无外部依赖的纯模块实现方案

纯Python实现热敏收据打印的可行方案

方案1:使用escpos库(跨平台,针对ESC/POS热敏打印机)

这个库专门适配热敏收据打印机,直接发送ESC/POS指令,文本以原生格式打印,不会转成图像,质量有保障,支持自定义纸张尺寸。

  • 安装:pip install python-escpos
  • 代码示例:
from escpos.printer import Usb, Serial, Network

# 根据打印机连接方式选择(示例为USB打印机)
# 需通过设备管理器(Windows)或lsusb(Linux)获取打印机的VID和PID
printer = Usb(0x0416, 0x5011)  # 替换为你的打印机实际VID、PID

# 设置纸张宽度(80mm宽的收据纸约对应576打印点)
printer.set(width=576)

# 打印收据内容
printer.text("欢迎光临\n")
printer.text("------------------------\n")
printer.text("商品:Python编程指南\n")
printer.text("价格:¥59.00\n")
printer.text("------------------------\n")
printer.text("合计:¥59.00\n")
printer.text("付款方式:微信支付\n")
printer.text("打印时间:2024-05-20 14:30\n")

# 执行切纸(需打印机支持该功能)
printer.cut()

# 关闭打印机连接
printer.close()
  • 优势:跨平台兼容Windows/Linux/macOS,直接控制打印机无外部依赖,支持条码、二维码、自定义字体等高级功能,纸张尺寸可精确设置。

方案2:Windows下用pywin32直接发送ESC/POS指令

Windows环境下,可通过pywin32直接与打印机端口通信,发送原生ESC/POS命令,完全基于Python库,不依赖任何外部程序。

  • 安装:pip install pywin32
  • 代码示例:
import win32print
import win32api

# 获取打印机句柄(可替换为指定打印机名称)
printer_name = win32print.GetDefaultPrinter()
hprinter = win32print.OpenPrinter(printer_name)

# 构造ESC/POS指令:设置行宽+打印内容+切纸
esc_pos_commands = b'\x1B\x57\x30'  # 设置行宽为576点(对应80mm纸宽)
esc_pos_commands += b'欢迎光临\r\n'
esc_pos_commands += b'------------------------\r\n'
esc_pos_commands += b'商品:Python编程指南\r\n'
esc_pos_commands += b'价格:¥59.00\r\n'
esc_pos_commands += b'------------------------\r\n'
esc_pos_commands += b'合计:¥59.00\r\n'
esc_pos_commands += b'付款方式:微信支付\r\n'
esc_pos_commands += b'打印时间:2024-05-20 14:30\r\n'
esc_pos_commands += b'\x1D\x56\x00'  # 切纸指令

# 发送打印任务
win32print.StartDocPrinter(hprinter, 1, ("热敏收据", None, "RAW"))
win32print.StartPagePrinter(hprinter)
win32print.WritePrinter(hprinter, esc_pos_commands)
win32print.EndPagePrinter(hprinter)
win32print.EndDocPrinter(hprinter)
win32print.ClosePrinter(hprinter)
  • 优势:完全依赖Python库,不调用外部查看器,直接发送原生指令,文本打印质量高,适合Windows环境下的定制化需求。

方案3:Linux/macOS下用pycups直接打印

针对Linux或macOS系统,可通过pycups库与系统CUPS打印服务交互,支持自定义纸张尺寸,纯Python实现。

  • 安装:pip install pycups(Linux需先安装系统依赖libcups2-dev)
  • 代码示例:
import cups
import tempfile
import os

# 连接CUPS服务器
conn = cups.Connection()

# 获取打印机列表,选择目标打印机(替换为你的打印机名称)
printers = conn.getPrinters()
printer_name = list(printers.keys())[0]

# 自定义纸张尺寸(示例为80mm宽、297mm长的收据纸)
media_size = 'Custom.80x297mm'

# 构造收据内容
content = """欢迎光临
------------------------
商品:Python编程指南
价格:¥59.00
------------------------
合计:¥59.00
付款方式:微信支付
打印时间:2024-05-20 14:30
"""

# 创建临时文件保存打印内容
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f:
    f.write(content)
    temp_file = f.name

# 发送打印任务
conn.printFile(printer_name, temp_file, "热敏收据", {'media': media_size})

# 删除临时文件
os.unlink(temp_file)
  • 优势:利用系统原生打印服务,纯Python实现,支持多种纸张尺寸设置,适配Linux/macOS环境。

内容的提问来源于stack exchange,提问作者Knowledge thirst

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 12:07:22