Mercurial > hg > zebra
annotate zebra.py @ 29:63d1260cc64e 0.1.0
- class name is now 'Zebra' instead of 'zebra'
- Fix for missing win32print module in pypi
- Drop python 2 support
- use setuptools instead of distutils
- improve documentation
- Added reset(), reset_default(), autosense(), print_config_label() and
print_graphic() functions
author | Ben Croston <ben@croston.org> |
---|---|
date | Tue, 01 Sep 2020 15:57:24 +0100 |
parents | 7c132e01c281 |
children | f77ca0963d6d |
rev | line source |
---|---|
29
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
1 #!/usr/bin/env python3 |
25 | 2 |
29
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
3 # Copyright (c) 2011-2020 Ben Croston |
25 | 4 # |
5 # Permission is hereby granted, free of charge, to any person obtaining a copy of | |
6 # this software and associated documentation files (the "Software"), to deal in | |
7 # the Software without restriction, including without limitation the rights to | |
8 # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | |
9 # of the Software, and to permit persons to whom the Software is furnished to do | |
10 # so, subject to the following conditions: | |
11 # | |
12 # The above copyright notice and this permission notice shall be included in all | |
13 # copies or substantial portions of the Software. | |
14 # | |
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 # SOFTWARE. | |
22 | |
0 | 23 import os.path |
24 import sys | |
10 | 25 |
0 | 26 if sys.platform.lower().startswith('win'): |
10 | 27 IS_WINDOWS = True |
0 | 28 import win32print |
10 | 29 else: |
30 IS_WINDOWS = False | |
22 | 31 import subprocess |
0 | 32 |
29
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
33 class Zebra: |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
34 """A class to communicate with (Zebra) label printers""" |
10 | 35 |
0 | 36 def __init__(self, queue=None): |
10 | 37 """queue - name of the printer queue (optional)""" |
0 | 38 self.queue = queue |
39 | |
40 def _output_unix(self, commands): | |
41 if self.queue == 'zebra_python_unittest': | |
42 p = subprocess.Popen(['cat','-'], stdin=subprocess.PIPE) | |
43 else: | |
27 | 44 p = subprocess.Popen(['lpr','-P{}'.format(self.queue),'-oraw'], stdin=subprocess.PIPE) |
17
a9dacd180597
Does not work in python 2.6 after all
Ben Croston <ben@croston.org>
parents:
14
diff
changeset
|
45 p.communicate(commands) |
0 | 46 p.stdin.close() |
47 | |
48 def _output_win(self, commands): | |
8 | 49 if self.queue == 'zebra_python_unittest': |
29
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
50 print(commands) |
8 | 51 return |
52 hPrinter = win32print.OpenPrinter(self.queue) | |
53 try: | |
54 hJob = win32print.StartDocPrinter(hPrinter, 1, ('Label',None,'RAW')) | |
55 try: | |
56 win32print.StartPagePrinter(hPrinter) | |
57 win32print.WritePrinter(hPrinter, commands) | |
58 win32print.EndPagePrinter(hPrinter) | |
59 finally: | |
60 win32print.EndDocPrinter(hPrinter) | |
61 finally: | |
62 win32print.ClosePrinter(hPrinter) | |
26 | 63 |
29
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
64 def output(self, commands, encoding='cp437'): |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
65 """Send raw commands to the label printer |
10 | 66 |
29
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
67 commands - commands to send to the printer. Converted to a byte string if necessary. |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
68 encoding - Encoding used if 'commands' is not a byte string |
10 | 69 """ |
0 | 70 assert self.queue is not None |
29
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
71 if type(commands) != bytes: |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
72 commands = str(commands).encode(encoding=encoding) |
10 | 73 if IS_WINDOWS: |
0 | 74 self._output_win(commands) |
75 else: | |
14 | 76 self._output_unix(commands) |
0 | 77 |
29
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
78 def print_config_label(self): |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
79 """ |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
80 Send an EPL2 command to print label(s) with current config settings |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
81 """ |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
82 self.output('\nU\n') |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
83 |
0 | 84 def _getqueues_unix(self): |
85 queues = [] | |
26 | 86 try: |
87 output = subprocess.check_output(['lpstat','-p'], universal_newlines=True) | |
88 except subprocess.CalledProcessError: | |
89 return [] | |
0 | 90 for line in output.split('\n'): |
91 if line.startswith('printer'): | |
92 queues.append(line.split(' ')[1]) | |
93 return queues | |
94 | |
95 def _getqueues_win(self): | |
8 | 96 printers = [] |
97 for (a,b,name,d) in win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL): | |
98 printers.append(name) | |
99 return printers | |
0 | 100 |
101 def getqueues(self): | |
10 | 102 """Returns a list of printer queues on local machine""" |
103 if IS_WINDOWS: | |
0 | 104 return self._getqueues_win() |
105 else: | |
106 return self._getqueues_unix() | |
107 | |
1 | 108 def setqueue(self, queue): |
10 | 109 """Set the printer queue""" |
0 | 110 self.queue = queue |
111 | |
2
ad9d9bf61243
Change direct transfer to direct thermal
Ben Croston <ben@fuzzyduckbrewery.co.uk>
parents:
1
diff
changeset
|
112 def setup(self, direct_thermal=None, label_height=None, label_width=None): |
29
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
113 """Set up the label printer using EPL2. Parameters are not set if they are None. |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
114 Not necessary if using AutoSense (hold feed button while powering on) |
10 | 115 |
116 direct_thermal - True if using direct thermal labels | |
117 label_height - tuple (label height, label gap) in dots | |
118 label_width - in dots | |
119 """ | |
0 | 120 commands = '\n' |
2
ad9d9bf61243
Change direct transfer to direct thermal
Ben Croston <ben@fuzzyduckbrewery.co.uk>
parents:
1
diff
changeset
|
121 if direct_thermal: |
29
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
122 commands += 'OD\n' |
0 | 123 if label_height: |
29
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
124 commands += 'Q%s,%s\n'%(label_height[0],label_height[1]) |
0 | 125 if label_width: |
29
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
126 commands += 'q%s\n'%label_width |
0 | 127 self.output(commands) |
128 | |
29
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
129 def reset_default(self): |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
130 """Reset the printer to factory settings using EPL2""" |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
131 self.output('\n^default\n') |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
132 |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
133 def reset(self): |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
134 """Resets the printer using EPL2 - equivalent to switching off/on""" |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
135 self.output('\n^@\n') |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
136 |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
137 def autosense(self): |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
138 """Run AutoSense by sending an EPL2 command |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
139 Get the printer to detect label and gap length and set the sensor levels |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
140 """ |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
141 self.output('\nxa\n') |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
142 |
0 | 143 def store_graphic(self, name, filename): |
29
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
144 """Store a 1 bit PCX file on the label printer, using EPL2. |
10 | 145 |
146 name - name to be used on printer | |
147 filename - local filename | |
148 """ | |
149 assert filename.lower().endswith('.pcx') | |
0 | 150 commands = '\nGK"%s"\n'%name |
151 commands += 'GK"%s"\n'%name | |
152 size = os.path.getsize(filename) | |
153 commands += 'GM"%s"%s\n'%(name,size) | |
154 self.output(commands) | |
155 self.output(open(filename,'rb').read()) | |
156 | |
29
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
157 def print_graphic(self, x, y, width, length, data, qty): |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
158 """Print a label from 1 bit data, using EPL2 |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
159 |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
160 x,y - top left coordinates of the image, in dots |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
161 width - width of image, in dots. Must be a multiple of 8. |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
162 length - length of image, in dots |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
163 data - raw graphical data, in bytes |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
164 qty - number of labels to print |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
165 """ |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
166 assert type(data) == bytes |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
167 assert width % 8 == 0 # make sure width is a multiple of 8 |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
168 assert (width//8) * length == len(data) |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
169 commands = b"\nN\nGW%d,%d,%d,%d,%s\nP%d\n"%(x, y, width//8, length, data, qty) |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
170 self.output(commands) |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
171 |
0 | 172 if __name__ == '__main__': |
29
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
173 z = Zebra() |
63d1260cc64e
- class name is now 'Zebra' instead of 'zebra'
Ben Croston <ben@croston.org>
parents:
27
diff
changeset
|
174 print('Printer queues found:',z.getqueues()) |
0 | 175 z.setqueue('zebra_python_unittest') |
2
ad9d9bf61243
Change direct transfer to direct thermal
Ben Croston <ben@fuzzyduckbrewery.co.uk>
parents:
1
diff
changeset
|
176 z.setup(direct_thermal=True, label_height=(406,32), label_width=609) # 3" x 2" direct thermal label |
1 | 177 z.store_graphic('logo','logo.pcx') |
0 | 178 label = """ |
179 N | |
180 GG419,40,"logo" | |
181 A40,80,0,4,1,1,N,"Tangerine Duck 4.4%" | |
182 A40,198,0,3,1,1,N,"Duty paid on 39.9l" | |
1 | 183 A40,240,0,3,1,1,N,"Gyle: 127 Best Before: 16/09/2011" |
0 | 184 A40,320,0,4,1,1,N,"Pump & Truncheon" |
185 P1 | |
186 """ | |
187 z.output(label) | |
188 |