Generating a Postnet barcode: Difference between revisions
(new script added) |
m (Geishi ALL THE THINGS!) |
||
(16 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
[[Category:Scripts]] | |||
{{Scripting Index}} | |||
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 = | 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: | ||
<tt>S = scribus.valueDialog('Postnet Barcode','Enter your Zip or Zip+4')</tt> | |||
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 = 47, Y = 323 (points) [postnet.py uses X = 130, Y = 130] -- 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. | 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. | ||
The scripts have been corrected to add "correction character" to the code, per USPS specs. | |||
<syntaxhighlight lang="python"> | |||
#!/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() | |||
</syntaxhighlight> | |||
Here is postnet.py -- uses Tkinter to give you a requestor for the entry. | |||
===<font color=green>This version now also has the correction character</font>=== | |||
<syntaxhighlight lang="python"> | |||
#!/usr/bin/env python | |||
# File: postnet.py | # File: postnet.py | ||
# originally | # originally 2006.02.26 Gregory Pittman | ||
# 2006.03.08 now has correction code | |||
import scribus | import scribus | ||
Line 33: | Line 121: | ||
relx=130 #Start X | relx=130 #Start X | ||
rely=130 #Start Y | rely=130 #Start Y | ||
correctnum = 0 | |||
if scribus.haveDoc(): | if scribus.haveDoc(): | ||
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 framing long line | ||
scribus.setLineWidth(a, d) | scribus.setLineWidth(a, d) | ||
scribus.setLineColor(b, d) | scribus.setLineColor(b, d) | ||
Line 42: | Line 131: | ||
rely = rely + 3.4 | rely = rely + 3.4 | ||
for x in S[0:]: | for x in S[0:]: | ||
if x.isdigit(): # Excludes nondigit entries | |||
xnum = int(x) | xnum = int(x) | ||
correctnum += xnum # Building the sum of all digits | |||
code = postcode[xnum] | code = postcode[xnum] | ||
for y in code[0:]: | for y in code[0:]: | ||
Line 58: | Line 148: | ||
scribus.setFillColor(b, d) | scribus.setFillColor(b, d) | ||
rely = rely + 3.4 | rely = rely + 3.4 | ||
d = scribus.createLine(relx,rely,relx+9,rely,) #long line | 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 framing long line | |||
scribus.setLineWidth(a, d) | scribus.setLineWidth(a, d) | ||
scribus.setLineColor(b, d) | scribus.setLineColor(b, d) | ||
Line 70: | Line 178: | ||
z = ImageDialog(root) | z = ImageDialog(root) | ||
root.wait_window(z) | root.wait_window(z) | ||
</ | </syntaxhighlight> |
Latest revision as of 14:06, 24 November 2013
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 = 47, Y = 323 (points) [postnet.py uses X = 130, Y = 130] -- 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.
The scripts have 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.
This version now also has the correction character
#!/usr/bin/env python
# File: postnet.py
# originally 2006.02.26 Gregory Pittman
# 2006.03.08 now has correction code
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
correctnum = 0
if scribus.haveDoc():
scribus.setRedraw(1)
scribus.setUnit(0)
d = scribus.createLine(relx,rely,relx+9,rely,) #Beginning framing 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(): # Excludes nondigit entries
xnum = int(x)
correctnum += xnum # Building the sum of all 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 framing 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)