控制程序
from PyQt5.QtPrintSupport import QPrinterInfo
import socket
import os
from jinja2 import Template
template = """
SIZE 50 mm,90 mm
GAP 2 mm,0 mm
REFERENCE 0,0
SPEED 2.5
DENSITY 3
SET PEEL OFF
SET CUTTER OFF
SET PARTIAL_CUTTER OFF
SET TEAR ON
DIRECTION 0
SHIFT 0
OFFSET 0 mm
CLS
TEXT 580,1030,"TSS24.BF2",180,1,1,"箱号:"
TEXT 580,1000,"TSS24.BF2",180,1,1,"{{ boxid }}"
TEXT 580,970,"TSS24.BF2",180,1,1,"数量:"
TEXT 580,940,"TSS24.BF2",180,1,1,"{{ cnt }}PCS"
TEXT 580,910,"TSS24.BF2",180,1,1,"版本:"
TEXT 580,880,"TSS24.BF2",180,1,1,"{{ version }}"
TEXT 580,850,"TSS24.BF2",180,1,1,"装箱时间:"
TEXT 580,820,"TSS24.BF2",180,1,1,"{{ date }}"
TEXT 580,700,"TSS24.BF2",180,1,1,"箱内模块MAC"
QRCODE 380,1030,L,3,M,180,1,1,"A{{boxid}},{{cnt}},{{version}},{{date}}{% for mac in macs %},{{ mac }}{% endfor %}"
{{mac_label}}
PRINT 1,1
"""
def get_label_info(boxid, cnt, version, date, macs):
label = Template(template)
height = 680
temp = []
mac_label = ''
for index, mac in enumerate(macs, start=1):
temp.append(mac)
if index % 4 == 0:
height = height - 25
mac_label += ('''TEXT 570,%s,"1",180,1,1,"%s"\r\n''' % (height, ' '.join(temp)))
temp = []
return label.render(boxid=boxid, cnt=cnt, version=version, date=date, macs=macs, mac_label=mac_label)
def print_label_info(label):
with open('print_label.txt', 'w') as f:
f.write(label+'\r\n')
host_name = socket.gethostname()
printer_name = QPrinterInfo().defaultPrinterName()
printer = r"\\%s\\%s" % (host_name, printer_name)
cmd = r'copy %s "%s"' % ('print_label.txt', printer)
print(cmd)
flag = os.system(cmd)
if flag:
raise Exception('print label error')
boxid = 2201020001
cnt = 100
version = 'SPM_STA_RSDEI11'
date = 220102
macs = ['QWERTY778899', 'UIOPAS112233', 'DFGHJK445566', 'LZXCVB112233'] * 25
label = get_label_info(boxid, cnt, version, date, macs)
print_label_info(label)