Mercurial > hg > zebra
annotate zebra.py @ 17:a9dacd180597
Does not work in python 2.6 after all
Tidy up
author | Ben Croston <ben@croston.org> |
---|---|
date | Mon, 15 Aug 2011 20:18:38 +0100 |
parents | e6ea86e3d0e1 |
children | 117c2aac83e5 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 | |
3 import subprocess | |
4 import os.path | |
5 import sys | |
10 | 6 |
0 | 7 if sys.platform.lower().startswith('win'): |
10 | 8 IS_WINDOWS = True |
0 | 9 import win32print |
10 | 10 else: |
11 IS_WINDOWS = False | |
0 | 12 |
13 class zebra(object): | |
10 | 14 """A class to communicate with (Zebra) label printers using EPL2""" |
15 | |
0 | 16 def __init__(self, queue=None): |
10 | 17 """queue - name of the printer queue (optional)""" |
0 | 18 self.queue = queue |
19 | |
20 def _output_unix(self, commands): | |
21 if self.queue == 'zebra_python_unittest': | |
22 p = subprocess.Popen(['cat','-'], stdin=subprocess.PIPE) | |
23 else: | |
24 p = subprocess.Popen(['lpr','-P%s'%self.queue], stdin=subprocess.PIPE) | |
17
a9dacd180597
Does not work in python 2.6 after all
Ben Croston <ben@croston.org>
parents:
14
diff
changeset
|
25 p.communicate(commands) |
0 | 26 p.stdin.close() |
27 | |
28 def _output_win(self, commands): | |
8 | 29 if self.queue == 'zebra_python_unittest': |
30 print commands | |
31 return | |
32 hPrinter = win32print.OpenPrinter(self.queue) | |
33 try: | |
34 hJob = win32print.StartDocPrinter(hPrinter, 1, ('Label',None,'RAW')) | |
35 try: | |
36 win32print.StartPagePrinter(hPrinter) | |
37 win32print.WritePrinter(hPrinter, commands) | |
38 win32print.EndPagePrinter(hPrinter) | |
39 finally: | |
40 win32print.EndDocPrinter(hPrinter) | |
41 finally: | |
42 win32print.ClosePrinter(hPrinter) | |
0 | 43 |
44 def output(self, commands): | |
10 | 45 """Output EPL2 commands to the label printer |
46 | |
47 commands - EPL2 commands to send to the printer | |
48 """ | |
0 | 49 assert self.queue is not None |
17
a9dacd180597
Does not work in python 2.6 after all
Ben Croston <ben@croston.org>
parents:
14
diff
changeset
|
50 if sys.version_info[0] == 3: |
a9dacd180597
Does not work in python 2.6 after all
Ben Croston <ben@croston.org>
parents:
14
diff
changeset
|
51 if type(commands) != bytes: |
a9dacd180597
Does not work in python 2.6 after all
Ben Croston <ben@croston.org>
parents:
14
diff
changeset
|
52 commands = str(commands).encode() |
a9dacd180597
Does not work in python 2.6 after all
Ben Croston <ben@croston.org>
parents:
14
diff
changeset
|
53 else: |
a9dacd180597
Does not work in python 2.6 after all
Ben Croston <ben@croston.org>
parents:
14
diff
changeset
|
54 commands = str(commands).encode() |
10 | 55 if IS_WINDOWS: |
0 | 56 self._output_win(commands) |
57 else: | |
14 | 58 self._output_unix(commands) |
0 | 59 |
60 def _getqueues_unix(self): | |
61 queues = [] | |
62 output = subprocess.check_output(['lpstat','-p'], universal_newlines=True) | |
63 for line in output.split('\n'): | |
64 if line.startswith('printer'): | |
65 queues.append(line.split(' ')[1]) | |
66 return queues | |
67 | |
68 def _getqueues_win(self): | |
8 | 69 printers = [] |
70 for (a,b,name,d) in win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL): | |
71 printers.append(name) | |
72 return printers | |
0 | 73 |
74 def getqueues(self): | |
10 | 75 """Returns a list of printer queues on local machine""" |
76 if IS_WINDOWS: | |
0 | 77 return self._getqueues_win() |
78 else: | |
79 return self._getqueues_unix() | |
80 | |
1 | 81 def setqueue(self, queue): |
10 | 82 """Set the printer queue""" |
0 | 83 self.queue = queue |
84 | |
2
ad9d9bf61243
Change direct transfer to direct thermal
Ben Croston <ben@fuzzyduckbrewery.co.uk>
parents:
1
diff
changeset
|
85 def setup(self, direct_thermal=None, label_height=None, label_width=None): |
10 | 86 """Set up the label printer. Parameters are not set if they are None. |
87 | |
88 direct_thermal - True if using direct thermal labels | |
89 label_height - tuple (label height, label gap) in dots | |
90 label_width - in dots | |
91 """ | |
0 | 92 commands = '\n' |
2
ad9d9bf61243
Change direct transfer to direct thermal
Ben Croston <ben@fuzzyduckbrewery.co.uk>
parents:
1
diff
changeset
|
93 if direct_thermal: |
0 | 94 commands += ('OD\n') |
95 if label_height: | |
96 commands += ('Q%s,%s\n'%(label_height[0],label_height[1])) | |
97 if label_width: | |
98 commands += ('q%s\n'%label_width) | |
99 self.output(commands) | |
100 | |
101 def store_graphic(self, name, filename): | |
10 | 102 """Store a .PCX file on the label printer |
103 | |
104 name - name to be used on printer | |
105 filename - local filename | |
106 """ | |
107 assert filename.lower().endswith('.pcx') | |
0 | 108 commands = '\nGK"%s"\n'%name |
109 commands += 'GK"%s"\n'%name | |
110 size = os.path.getsize(filename) | |
111 commands += 'GM"%s"%s\n'%(name,size) | |
112 self.output(commands) | |
113 self.output(open(filename,'rb').read()) | |
114 | |
115 if __name__ == '__main__': | |
116 z = zebra() | |
117 print 'Printer queues found:',z.getqueues() | |
118 z.setqueue('zebra_python_unittest') | |
2
ad9d9bf61243
Change direct transfer to direct thermal
Ben Croston <ben@fuzzyduckbrewery.co.uk>
parents:
1
diff
changeset
|
119 z.setup(direct_thermal=True, label_height=(406,32), label_width=609) # 3" x 2" direct thermal label |
1 | 120 z.store_graphic('logo','logo.pcx') |
0 | 121 label = """ |
122 N | |
123 GG419,40,"logo" | |
124 A40,80,0,4,1,1,N,"Tangerine Duck 4.4%" | |
125 A40,198,0,3,1,1,N,"Duty paid on 39.9l" | |
1 | 126 A40,240,0,3,1,1,N,"Gyle: 127 Best Before: 16/09/2011" |
0 | 127 A40,320,0,4,1,1,N,"Pump & Truncheon" |
128 P1 | |
129 """ | |
130 z.output(label) | |
131 |