Generating a Postnet barcode: Difference between revisions
mNo edit summary |
(postnet2.py updated) |
||
Line 13: | Line 13: | ||
To relocate the barcode, make the entire barcode a group, so that you can drag it wherever you like or use Properties to place it. | To relocate the barcode, make the entire barcode a group, so that you can drag it wherever you like or use Properties to place it. | ||
This script has been corrected to add "correction character" to the code, per USPS specs. | |||
<pre> | <pre> | ||
#!/usr/bin/env python | #!/usr/bin/env python | ||
# File: postnet2.py | # File: postnet2.py | ||
# originally 2006.03.06 Gregory Pittman | # originally 2006.03.06 Gregory Pittman | ||
# this version 2006.03.07 | |||
# adds the correction code: | |||
# the sum of all the digits | |||
# must be divisible by 10 | |||
import scribus | import scribus | ||
Line 24: | Line 29: | ||
a = 1.44 #line width | a = 1.44 #line width | ||
b="Black" #line color | b="Black" #line color | ||
relx= | relx=47 #Start X | ||
rely= | rely=323 #Start Y | ||
correctnum = 0 | |||
if scribus.haveDoc(): | if scribus.haveDoc(): | ||
S = scribus.valueDialog('Postnet Barcode','Enter your Zip or Zip+4') | S = scribus.valueDialog('Postnet Barcode','Enter your Zip or Zip+4') | ||
scribus.setRedraw(1) | scribus.setRedraw(1) | ||
scribus.setUnit(0) | scribus.setUnit(0) | ||
d = scribus.createLine(relx,rely,relx+9,rely,) # | d = scribus.createLine(relx,rely,relx+9,rely,) #Beginning long framing line | ||
scribus.setLineWidth(a, d) | scribus.setLineWidth(a, d) | ||
scribus.setLineColor(b, d) | scribus.setLineColor(b, d) | ||
Line 38: | Line 44: | ||
if x.isdigit(): | if x.isdigit(): | ||
xnum = int(x) | xnum = int(x) | ||
correctnum += xnum # Building the sum of all the digits | |||
code = postcode[xnum] | code = postcode[xnum] | ||
for y in code[0:]: | for y in code[0:]: | ||
Line 52: | Line 59: | ||
scribus.setFillColor(b, d) | scribus.setFillColor(b, d) | ||
rely = rely + 3.4 | rely = rely + 3.4 | ||
d = scribus.createLine(relx,rely,relx+9,rely,) # | correctstr = str(correctnum) # Here is where the correction code is added | ||
correctdig = int(correctstr[-1]) | |||
if correctdig != 0: | |||
correctdig = 10 - correctdig | |||
code = postcode[correctdig] | |||
for y in code[0:]: | |||
if y == 'l': | |||
d = scribus.createLine(relx,rely,relx+9,rely,) #Long line | |||
scribus.setLineWidth(a, d) | |||
scribus.setLineColor(b, d) | |||
scribus.setFillColor(b, d) | |||
rely = rely + 3.4 | |||
elif y == 's': | |||
d = scribus.createLine(relx,rely,relx+3.6,rely,) #short line | |||
scribus.setLineWidth(a, d) | |||
scribus.setLineColor(b, d) | |||
scribus.setFillColor(b, d) | |||
rely = rely + 3.4 | |||
d = scribus.createLine(relx,rely,relx+9,rely,) #Ending long framing line | |||
scribus.setLineWidth(a, d) | scribus.setLineWidth(a, d) | ||
scribus.setLineColor(b, d) | scribus.setLineColor(b, d) | ||
Line 60: | Line 86: | ||
</pre> | </pre> | ||
Here is postnet.py -- uses Tkinter to give you a requestor for the entry. | Here is postnet.py -- uses Tkinter to give you a requestor for the entry. | ||
<i>Caution: this has yet to have the correction character added.</i> | |||
<pre> | <pre> | ||
#!/usr/bin/env python | #!/usr/bin/env python |
Revision as of 04:02, 8 March 2006
This first script is a new version (postnet2.py) which does without the complexity of Tkinter, by using 'valueDialog()' instead. If you compare the two scripts, this one is much simpler. That one line:
S = scribus.valueDialog('Postnet Barcode','Enter your Zip or Zip+4')
replaces ALL the Tkinter material.
Both scripts will generate a Postnet barcode (US Postal Service) for a Zipcode that you enter. You may enter your numbers and include hyphens if you wish, since these and any other non-numeric characters are ignored.
The barcode is a series of lines drawn to USPS specifications, and in this script is placed vertically (meaning the lines themselves are horizonal), with the beginning of the barcode toward the top of the page at position X = 130, Y = 130 (points) -- see the lines with comments 'Start X' and 'Start Y'.
To relocate the barcode, make the entire barcode a group, so that you can drag it wherever you like or use Properties to place it. This script has been corrected to add "correction character" to the code, per USPS specs.
#!/usr/bin/env python # File: postnet2.py # originally 2006.03.06 Gregory Pittman # this version 2006.03.07 # adds the correction code: # the sum of all the digits # must be divisible by 10 import scribus postcode = ['llsss','sssll','sslsl','sslls','slssl','slsls','sllss','lsssl','lssls','lslss'] a = 1.44 #line width b="Black" #line color relx=47 #Start X rely=323 #Start Y correctnum = 0 if scribus.haveDoc(): S = scribus.valueDialog('Postnet Barcode','Enter your Zip or Zip+4') scribus.setRedraw(1) scribus.setUnit(0) d = scribus.createLine(relx,rely,relx+9,rely,) #Beginning long framing line scribus.setLineWidth(a, d) scribus.setLineColor(b, d) scribus.setFillColor(b, d) rely = rely + 3.4 for x in S[0:]: if x.isdigit(): xnum = int(x) correctnum += xnum # Building the sum of all the digits code = postcode[xnum] for y in code[0:]: if y == 'l': d = scribus.createLine(relx,rely,relx+9,rely,) #Long line scribus.setLineWidth(a, d) scribus.setLineColor(b, d) scribus.setFillColor(b, d) rely = rely + 3.4 elif y == 's': d = scribus.createLine(relx,rely,relx+3.6,rely,) #short line scribus.setLineWidth(a, d) scribus.setLineColor(b, d) scribus.setFillColor(b, d) rely = rely + 3.4 correctstr = str(correctnum) # Here is where the correction code is added correctdig = int(correctstr[-1]) if correctdig != 0: correctdig = 10 - correctdig code = postcode[correctdig] for y in code[0:]: if y == 'l': d = scribus.createLine(relx,rely,relx+9,rely,) #Long line scribus.setLineWidth(a, d) scribus.setLineColor(b, d) scribus.setFillColor(b, d) rely = rely + 3.4 elif y == 's': d = scribus.createLine(relx,rely,relx+3.6,rely,) #short line scribus.setLineWidth(a, d) scribus.setLineColor(b, d) scribus.setFillColor(b, d) rely = rely + 3.4 d = scribus.createLine(relx,rely,relx+9,rely,) #Ending long framing line scribus.setLineWidth(a, d) scribus.setLineColor(b, d) scribus.setFillColor(b, d) scribus.redrawAll()
Here is postnet.py -- uses Tkinter to give you a requestor for the entry. Caution: this has yet to have the correction character added.
#!/usr/bin/env python # File: postnet.py # originally created 2006.02.26 - Gregory Pittman import scribus import Tkinter class ImageDialog(Tkinter.Toplevel): def __init__(self, parent): Tkinter.Toplevel.__init__(self, parent, bg="#bbbbff") Tkinter.Label(self, text='Enter the Postal Code, Click OK',bg="#bbbbff").grid(row=0,columnspan=6) self.e = Tkinter.Entry(self) self.e.grid(row=1,columnspan=6) b = Tkinter.Button(self, text='OK', bg="#55ff88", command=self.ok) b.grid(row=2,columnspan=6) self.protocol("WM_DELETE_WINDOW", self.quit) def ok(self): S = self.e.get() m = Tkinter.Message(root, text=self.e.get()+'\nClose this\n window') m.grid(row=0, columnspan=4) postcode = ['llsss','sssll','sslsl','sslls','slssl','slsls','sllss','lsssl','lssls','lslss'] a = 1.44 #line width b="Black" #line color relx=130 #Start X rely=130 #Start Y if scribus.haveDoc(): scribus.setRedraw(1) scribus.setUnit(0) d = scribus.createLine(relx,rely,relx+9,rely,) #Long line scribus.setLineWidth(a, d) scribus.setLineColor(b, d) scribus.setFillColor(b, d) rely = rely + 3.4 for x in S[0:]: if x.isdigit(): xnum = int(x) code = postcode[xnum] for y in code[0:]: if y == 'l': d = scribus.createLine(relx,rely,relx+9,rely,) #Long line scribus.setLineWidth(a, d) scribus.setLineColor(b, d) scribus.setFillColor(b, d) rely = rely + 3.4 elif y == 's': d = scribus.createLine(relx,rely,relx+3.6,rely,) #short line scribus.setLineWidth(a, d) scribus.setLineColor(b, d) scribus.setFillColor(b, d) rely = rely + 3.4 d = scribus.createLine(relx,rely,relx+9,rely,) #long line scribus.setLineWidth(a, d) scribus.setLineColor(b, d) scribus.setFillColor(b, d) redrawAll() self.Tkinter.Toplevel.destroy() root = Tkinter.Tk() root.update() z = ImageDialog(root) root.wait_window(z)