使用最少的文档在EscaposPython上打印繁体中文字符

2024-09-26 22:51:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我还是escpos编程新手,所以如果有愚蠢的错误等,请容忍我

如果有人能告诉我如何使用ec-pm-80330打印机上的escpos python库打印繁体汉字,我会很高兴,因为我只能在网上找到功能列表http://www.ecline.com.hk/admin/upload/product/dataSheet/64.pdf

我一直在用不同类型的编码强制打印,并试图打开汉字模式,但没有任何效果

下面是我解决问题的一个尝试(简化)

from escpos import printer as escpos_printer
from escpos.constants import FS, NUL

menu = {'name': "你好"}

printer = escpos_printer.Serial(devfile=comport)

printer.textln('Test GB18030')
printer._raw(menu['name'].encode('GB18030'))
printer.ln(1)

printer.textln('Test UTF8')
printer._raw(menu['name'].encode('utf8'))
printer.ln(1)

printer.charcode(code='CP850')
printer.textln('Test CP850 - raw')
printer._raw(menu['name'].encode('utf8'))
printer.ln(1)

printer.textln('Test CP850 - text')
printer.textln(menu['name'])
printer.charcode(code='AUTO')

printer.textln('Test normal')
printer.textln(menu['name'])


# Turned on kanji mode
printer.textln('Kanji mode on')
printer._raw(FS + '!'.encode('ascii') + NUL)
printer.textln('Test GB18030')
printer._raw(menu['name'].encode('GB18030'))
printer.ln(1)

printer.textln('Test UTF8')
printer._raw(menu['name'].encode('utf8'))
printer.ln(1)
        
printer.charcode(code='CP850')
printer.textln('Test CP850 - raw')
printer._raw(menu['name'].encode('utf8'))
printer.ln(1)

printer.textln('Test CP850 - text')
printer.textln(menu['name'])
printer.charcode(code='AUTO')

printer.textln('Test normal')
printer.textln(menu['name'])

printer._raw(FS + '.'.encode('ascii'))

我也尝试过使用gbk和big5,它们都会在打印结果中给出“?”或胡言乱语,但除了汉字之外,效果很好

实际上,打印机后面列出了一些命令,我不确定它是否能帮助我解决这个问题

Commands at the back of the printer

从stack overflow/github中的其他答案来看,这对我没有多大帮助,但如果有人愿意给我一个解释,说明我遗漏了或错了哪一部分,我会很高兴

提前谢谢


Tags: nametestrawcodeutf8fsprinterencode

热门问题