Search code examples
pythonprintingzebra-printerszpl

Trying to print using ZPL to zebra printer


So im trying to print using ZPL language to a zebra printer, but when i run the below code all i get is blank labels, any idea why its not reading the ZPL variable "label". I have put the code into labelary and it looks correct but i cant get it to print the ZPL its just blank labels.

    import os
from PIL import Image
import zpl
from zebra import Zebra

lines = []
with open(
        r'C:\Users\matthew.vandruff\Downloads\Hard Drives (version '
        r'1)-b0dd6970-7dfd-4bb7-b04d-fc9c3ff4bc8a-a54c9c76-f7e2-40b1-8ff4-6d7eec1c99bb.csv') as f:
    for line in f.readlines():
        l, name = line.strip().split(',')
        lines.append((l, name))

list = lines
x = 1
while x < len(list):
    HD_Serial, HD_Number = list[x]
    # print(HD_Serial, HD_Number)
    x += 1

l = zpl.Label(100, 60)
height = 0
height += 5
l.origin(30, height)
l.write_barcode(height=70, barcode_type='C', check_digit='Y')
l.write_text(f'{HD_Number}')
l.endorigin()

height += 0
image_width = 12
l.origin((l.width - image_width) / 3, height)
image_height = l.write_graphic(
    Image.open(os.path.join(os.path.dirname(zpl.__file__), 'MTC_Logo.png')),
    image_width)
l.endorigin()

height += image_height + 5
l.origin(15, height)
l.write_barcode(height=70, barcode_type='C', check_digit='Y')
l.write_text(f'{HD_Serial}')
l.endorigin()

print(l.dumpZPL())

z = Zebra()
Q = z.getqueues()
z.setqueue(Q[0])
z.setup()
z.autosense()
z.output(l)

Solution

  • So I'm guessing my issue was in the settings of the printer but this is my final working code for anyone that comes looking and is only getting blanks when trying to print.

    import os
    from PIL import Image
    import zpl
    from zebra import Zebra
    
    lines = []
    with open(
            r'C:\Users\matthew.vandruff\Downloads\Hard Drives (version '
            r'1)-b0dd6970-7dfd-4bb7-b04d-fc9c3ff4bc8a-a54c9c76-f7e2-40b1-8ff4-6d7eec1c99bb.csv') as f:
        for line in f.readlines():
            l, name = line.strip().split(',')
            lines.append((l, name))
    
    list = lines
    x = 1
    while x < len(list):
        HD_Serial, HD_Number = list[x]
        # print(HD_Serial, HD_Number)
        x += 1
    
    
    
    l = zpl.Label(100, 100)
    height = 0
    height += 15
    l.origin(23, height)
    l.write_barcode(height=80, barcode_type='C', check_digit='Y')
    l.write_text(f'{HD_Number}')
    l.endorigin()
    
    height += 0
    image_width = 12
    l.origin((l.width - image_width) / 10, height)
    image_height = l.write_graphic(
        Image.open(os.path.join(os.path.dirname(zpl.__file__), 'MTC_Logo.bmp')),
        image_width)
    l.endorigin()
    
    height += image_height + 5
    l.origin(5, height)
    l.write_barcode(height=80, barcode_type='C', check_digit='Y')
    l.write_text(f'{HD_Serial}')
    l.endorigin()
    
    label = l.dumpZPL()
    l.preview()
    
    z = Zebra()
    Q = z.getqueues()
    z.setqueue(Q[0])
    z.setup()
    z.output(label)