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