Quickie Python script to generate PDF “Tickets”

Here’s a quick little script that processes a CSV file of lastname,firstname data and generates a random ID. It then creates a PDF for each “ticket” with the relevant information, as well as a PDF key file that someone could use to check against. I’ve attached an image of the “ticket” that results, along with an excerpt from the key file.

The script uses the fantastic ReportLab library to generate PDFs and barcodes. The script contains examples of the basic use of paragraphs, images, and tables within ReportLab.  This was written in a hurry. There is no proper “__main__”, etc. I am not holding this out as an example of wonderful Python coding.

If you have questions, please just send me a note and I’ll be glad to go into more detail.

DEBUG = False

# Miscellaneous
from cStringIO import StringIO
import random
import re

# Process CSV files
import csv

# Generate PDFs using ReportLab
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import *
from reportlab.lib import colors
from reportlab.lib.units import inch
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import LETTER
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize
from reportlab.lib.units import inch, mm
from reportlab.graphics.barcode import code39

PAGE_HEIGHT= 11 * inch
PAGE_WIDTH= 8.5 * inch
styles = getSampleStyleSheet()
Title = "ELS Graduation Regalia Checklist"

def process_csv(fn):
    ''' Process a csv file containing lastname and firstname.
    Return a list of lists containing lastname, firstname, and random identifier
    data =  []
    choices = range(100000)
    csvfile = csv.reader(open(fn))
    # throw away header
    for row in csvfile:
        if DEBUG: print row
        newdata = [row[0],row[1],row[0][:3].upper() +'-'+str(random.choice(choices))]
        if DEBUG: print newdata
    return data

def docPage(canvas, doc):
    canvas.setFont('Times-Bold', 10)
    canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT - (.25 * inch), Title)
    canvas.drawString(7 * inch, .75 * inch, "Page %d" % (doc.page,))

def ticketPage(canvas, doc ):
    H = 1.5 * inch
    W = 1.5 * .69 * inch
    canvas.drawImage('emorylaw.jpg', 6 * inch, PAGE_HEIGHT - (1.75 * inch), width = W, height = H)

def gen_key(data, fn):
    doc = SimpleDocTemplate(fn)
    Story = []
    tabledata = []
    style = styles["Normal"]
    tabledata.append(['Graduating student','Regalia code','Picked up?'])
    for row in data:
        tabledata.append(["%s, %s" % (row[0], row[1]), "%s" % row[2],"[                ]"])
    t=Table(tabledata, colWidths= 2 * inch, repeatRows=1)
    doc.build(Story, onFirstPage=docPage, onLaterPages=docPage)

def gen_ticket(row,fn):
    "Generate a regalia ticket as a separate PDF document."
    doc = SimpleDocTemplate(fn)
    Story = []
    styleN = styles["Normal"]
    styleH = styles['Heading1']
    Story.append(Paragraph("ELS Graduation 2010 Regalia Ticket",styleH))
    Story.append(Spacer(1 * inch, .5 * inch))
Student name: %s, %s" % (row[0], row[1]), styleN))
    Story.append(Spacer(1 * inch, .25 * inch))
Student regalia code: %s" % (row[2]), styleN))
    Story.append(Spacer(1 * inch, .5 * inch))
    barcode=code39.Extended39(row[2], barWidth= 0.02 * inch, barHeight= .5 * inch)
    doc.build(Story, onFirstPage=ticketPage, onLaterPages=ticketPage)

# create an re to get rid of non-word chars for file names
pattern = re.compile('[W_]+')

data = process_csv('names.csv')
for row in data:
    fname =  pattern.sub('-', row[0] + '-'+ row[1]).lower()
    fname += ".pdf"
    gen_ticket(row, fname)
gen_key(data, "keyfile.pdf")
Reblog this post [with Zemanta]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s