Generating a Code39 Barcode
The reason for writing this script was that I had already written a script for modifying a form we use, and on this form are barcodes, one of which indicates the type of form, but the other is variable according to the content of the page.
The script creates that custom part, which has a 10-digit number, then underneath is the barcode. By trial and error I determined that this was a Code39 barcode. Unfortunately, the barcode generator in Scribus cannot be scripted, and in fact you cannot make a custom size for the barcode, so I was copying the 10-digit number, then running Barcode generator, then resizing the created code, which is a PostScript creation, so when it's resized, the width of the bars may change. So far it's been Ok, but this might affect the scanning process.
So here is a first attempt, code39.py. It is not very fast. Creating a 10-digit barcode takes 60-70 seconds. I suspect that either the parsing of the code for narrow and wide lines or the repetitive line drawing (or both) is the speed issue. So far this will only generate a code for digits (plus the start/stop characters). I'd like to see if I can speed it up before adding letter capability.
code39.py
#!/usr/bin/env python # File: code39.py # Based on postnet2.py originally 2006.03.06 Gregory Pittman # this version 2010.07.31 # So far will only encode numbers, plus the start/stop character import scribus code39 = ['nnswwn','wnsnnw','nwsnnw','wwsnnn','nnswnw','wnswnn','nwswnn','nnsnww','wnsnwn','nwsnwn'] startstop = 'nsnwwn' narrow = 0.8 # narrow line width wide = 2.4 # wide line width b="Black" #line color relx=200 #Start X rely=100 #Start Y if scribus.haveDoc(): S = scribus.valueDialog('Code 39','Enter Number') height = scribus.valueDialog('Code 39','Enter Height of Bars (Points)') height = int(height) scribus.setRedraw(1) scribus.setUnit(0) code = startstop for x in S[0:]: if x.isdigit(): xnum = int(x) code = code + code39[xnum] code = code + startstop for y in code[0:]: if y == 'n': relx = relx + narrow/2 d = scribus.createLine(relx,rely,relx,rely + height,) #narrow line scribus.setLineWidth(narrow, d) scribus.setLineColor(b, d) scribus.setFillColor(b, d) relx = relx + narrow + narrow/2 elif y == 'w': relx = relx + wide/2 d = scribus.createLine(relx,rely,relx,rely + height,) #wide line scribus.setLineWidth(wide, d) scribus.setLineColor(b, d) scribus.setFillColor(b, d) relx = relx + narrow + wide/2 elif y == 's': #for wide space relx = relx + wide scribus.redrawAll()