python 斑马打印模板
打印代码逻辑如下;
包括样式、表格
import win32printdef print_zpl_from_usb_printer(printer_name, zpl_content):# 打开打印机hPrinter = win32print.OpenPrinter(printer_name)if hPrinter is None:print(f"Failed to open printer: {printer_name}")return Falsetry:# 准备开始打印作业的信息job_id = win32print.StartDocPrinter(hPrinter, 1, ("Label", None, "RAW"))if job_id == 0:print("StartDocPrinter failed")return False# 开始页面win32print.StartPagePrinter(hPrinter)# 写入ZPL数据win32print.WritePrinter(hPrinter, zpl_content.encode('utf-8'))# 结束页面win32print.EndPagePrinter(hPrinter)# 结束打印作业win32print.EndDocPrinter(hPrinter)finally:# 总是关闭打印机句柄win32print.ClosePrinter(hPrinter)return Truezpl_content = "^XA^CI28^SEE:GB18030.DAT^FS^CWA,E:SIMSUN.FNT^FS^FO30,30^FD{}^FS^XZ".format('标贴Label d,我是一个')
zpl_content = """
^XA
^CI28
^SEE:GB18030.DAT
^FS
^CWA,E:SIMSUN.FNT
^FS
^FO5,5^GB589.4094488188977,469.52755905511816,3^FS
^FO5,63.83^GB589.4094488188977,,3^FS
^FO5,127.66^GB589.4094488188977,,3^FS
^FO5,186.49^GB589.4094488188977,,3^FS
^FO5,245.32^GB589.4094488188977,,3^FS
^FO5,304.15^GB389.40944881889766,,3^FS
^FO5,362.97^GB389.40944881889766,,3^FS
^FO5,421.8^GB389.40944881889766,,3^FS
^FO5,480.63^GB389.40944881889766,,3^FS
^FO140.40944881889766,63.83^GB,480.63,3^FS
^FO390.40944881889766,245.32^GB,304.15,3^FS
^FO200,40.0^AZN,^FD 晶振供应商 ^FS
^FO,100.95^AZN,^FD 物料编码 ^FS
^FO130,100.95^AZN,^FD 010010010005 ^FS
^FO,160.95^AZN,^FD 产品名称 ^FS
^FO130,160.95^AZN,^FD 贴片电阻(测试打印)111 ^FS
^FO,220.95^AZN,^FD 产品型号 ^FS
^FO130,220.95^AZN,^FD R0402 0RJ ^FS
^FO,280.95^AZN,^FD 采购单号 ^FS
^FO130,280.95^AZN,^FD P00176 ^FS
^FO,340.95^AZN,^FD 批次号 ^FS
^FO130,340.95^AZN,^FD ^FS
^FO,400.95^AZN,^FD 数量 ^FS
^FO130,400.95^AZN,^FD 15000.0 ^FS
^FO,450.95^AZN,^FD 日期 ^FS
^FO130,450.95^AZN,^FD 2024-09-19 12:13:39 ^FS
^FO395.40944881889766,190^BQN,2,3,0,^FDMA 'name':'电子原材料仓/IN/00053','move_line_ids_without_package.quantity':'15000.0','move_line_ids_without_package.lot_id':'','origin':'P00176' ^FS
^XZ
"""printer_logical_name = "ZDesigner ZD888-203dpi ZPL"
success = print_zpl_from_usb_printer(printer_logical_name, zpl_content)
if success:print("Print job sent successfully.")
else:print("Failed to send print job.")
打印效果如下: