Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

3/13/24, 12:10 PM Python: how to print a .lbl file on Zebra printer?

- Stack Overflow

Python: how to print a .lbl file on Zebra printer?


Asked 1 year, 8 months ago Modified 1 year, 8 months ago Viewed 2k times

I have a Zebra Network printer, and I need to print different labels, with variable informations
(for example: the date). I created all the labels with Zebra Designer and saved them as .lbl
0 files. I want to print them from a python interface, with a modification for the variable
information

How can I do that?

I know how to edit and print ZPL code, but I want to use the .lbl files so that a non-
programmer can easily create a new label.

import socket
mysocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host = "10.4.29.158"
port = 9100
try:
mysocket.connect((host, port)) #connecting to host
mysocket.send(b"^XA^FX^FDThis is a test^FS^XZ")#using bytes
mysocket.close () #closing connection
except:
print("Error with the connection")

python zebra-printers

Share Improve this question Follow asked Jun 24, 2022 at 14:03
Medestrac
27 4

I know nothing about zebra printers. Given that you say they are network-attached, I assume the code
you have shown is opening a socket and sending some data to a printer. I can only imagine you want
to read the contents of a .lbl file and send that instead? Is that your question? If so lbl =
open('SomeFile.lbl', 'rb').read() and then mysocket.sendall(lbl) – Mark Setchell Jun 24,
2022 at 15:32

Your real question is how to insert runtime values in ZPL code, isn't it!? – Delphi Coder Jun 27, 2022 at
9:09

The project is: a user create a mask in a label designing software, with some variable elements (for
example: the date) and save as .lbl file. Then a python script read that mask, writes the date in the
variable field, and send it to a printer. With open(file,'rb').read() I only get gibberish content, and can't
adjust the variable. – Medestrac Jun 27, 2022 at 9:34

I still know nothing about zebras. What happens if you use your normal program to create a label but
put some known placeholder string in the file where you want a variable. E.g. you want to print a name,
so in your normal software you create a label with name FUNKYNAME. Can you then see
FUNKYNAME in the label file and replace it? So read lbl per my previous comment then try if
'FUNKYNAME' in lbl: – Mark Setchell Jun 27, 2022 at 17:07

https://stackoverflow.com/questions/72745259/python-how-to-print-a-lbl-file-on-zebra-printer 1/4
3/13/24, 12:10 PM Python: how to print a .lbl file on Zebra printer? - Stack Overflow

When your file content is not plain (ZPL) text, then you can't just save it as .lbl in your label editor
because it seems to be a propietary format of that software. What you have to do is, print it to a plain
text/file printer! – Delphi Coder Jun 28, 2022 at 5:24

1 Answer Sorted by: Highest score (default)

Add a new printer to your Windows system:

1. Open the start menu, type printer and press Enter


0
2. Click Add a printer or scanner

3. Wait a a few moments until the message The printer that I want isn't listed
appears, then click on it

https://stackoverflow.com/questions/72745259/python-how-to-print-a-lbl-file-on-zebra-printer 2/4
3/13/24, 12:10 PM Python: how to print a .lbl file on Zebra printer? - Stack Overflow

4. In the next dialog select Add a local printer... and click Next

5. Then open the dropdown menu for exisiting ports, select FILE: ... and click Next

6. In the next screen on the left side select Generic , on the right side select Generic / Text
Only , then click Next

https://stackoverflow.com/questions/72745259/python-how-to-print-a-lbl-file-on-zebra-printer 3/4
3/13/24, 12:10 PM Python: how to print a .lbl file on Zebra printer? - Stack Overflow

7. Give the printer a name of your choice or keep the default, then click Next

8. Select if the printer needs to be shared or not, and click Next

9. If asked, don't select the printer as default, and click Finish

Now you can use this printer in your label design software to create plain text files containing
the ZPL code. Load these files in your python app and replace the search strings with the
desired content!

For further tests with the ZPL files you can use the online service Labelary.

Share Improve this answer Follow answered Jun 30, 2022 at 9:22
Delphi Coder
1,885 1 15 26

https://stackoverflow.com/questions/72745259/python-how-to-print-a-lbl-file-on-zebra-printer 4/4

You might also like