python 用于在Brother PT-H500,P700,E500标签打印机上打印多行文本的Python脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 用于在Brother PT-H500,P700,E500标签打印机上打印多行文本的Python脚本相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python3

# Based on https://github.com/SquirrelCZE/ptouch-770
# Trying to add multi-line capabilities.
# Font size calculation/positioning apparently not correct.
# On 12mm tape, the topmost 20px are not printed, the following 84 px are printed
# (as determined by experimentation using calibrate_12mm_tape.pbm made with GIMP)

from PIL import Image, ImageFont, ImageDraw
import argparse
import os

parser = argparse.ArgumentParser(description="Utility for printing on ptouch 700")
parser.add_argument('--text', type=str, help="string to be printed", default="Two\\nlines")
parser.add_argument('--height', type=int, help="height in pixels of loaded tape", default=84)
parser.add_argument('--max-height', type=int, help="maximal height in pixels for printer", default=104)
parser.add_argument('--rim', type=int, help="rim around the text in pixels", default=5)
parser.add_argument('--font', type=str, help="name of truetype font", default="FreeSansBold.ttf")

args = parser.parse_args()

text = args.text.split("\\n")
#text=['10/2018, drive pixels from 2A PSU', 'https://github.com/wvdv2002/ESP8266-LED-Websockets', 'NUM_LEDS=60, DATA_PIN=13', 'Adafruit_NeoPixel(NUM_LEDS, DATA_PIN, ', 'NEO_GRBW + NEO_KHZ800);']
print(text)

lines = len(text)

fontsize = int(round(args.height/lines,0))

font = ImageFont.truetype(args.font, fontsize)

# Determine the longest line
max_width = 0
for line in text:
    text_width, text_height = font.getsize(line)
    width = text_width + args.rim * 2
    if(width > max_width):
        max_width = width

image = Image.new("1", ( max_width, args.max_height ), 0)
draw = ImageDraw.Draw( image )

text_x = args.rim
text_y = -0
for line in text:
    text_y = text_y + text_height*0.9
    draw.text((text_x,text_y), line, fill=255, font=font)

image.save("out.pbm")

# os.system("./ptouch-770-write out.pbm ") # Need https://github.com/SquirrelCZE/ptouch-770

以上是关于python 用于在Brother PT-H500,P700,E500标签打印机上打印多行文本的Python脚本的主要内容,如果未能解决你的问题,请参考以下文章

在树莓派上使用 python 库 Brother_ql 打印

在树莓派上用python库brother_ql打印

fzoj1314 You are my brother

nyoj 282 You are my brother

尝试使用Android Brother Sdk打印无线时,ERROR_WRONG_LABEL用于标签打印机

远程监控显示brother数控机床数据