Created
April 15, 2014 13:16
-
-
Save anandology/10731697 to your computer and use it in GitHub Desktop.
voterlist2pdf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """Script to generate voterlist PDF. | |
| References: | |
| * https://pkimber.net/howto/python/modules/reportlab/header-footer.html | |
| * http://www.blog.pythonlibrary.org/2010/09/21/reportlab-tables-creating-tables-in-pdfs-with-python/ | |
| * http://www.hoboes.com/Mimsy/hacks/multiple-column-pdf/ | |
| """ | |
| from reportlab.platypus import BaseDocTemplate, Frame, PageTemplate, Table, TableStyle | |
| from reportlab.lib.pagesizes import A4, inch, landscape | |
| from reportlab.lib import colors | |
| import sys | |
| import json | |
| def make_doc(filename): | |
| doc = BaseDocTemplate(filename, showBoundary=0, pagesize=landscape(A4), | |
| rightMargin=48, | |
| leftMargin=48, | |
| topMargin=32, | |
| bottomMargin=32) | |
| #Two Columns | |
| frame1 = Frame(doc.leftMargin, doc.bottomMargin, doc.width/2-6, doc.height, id='col1') | |
| frame2 = Frame(doc.leftMargin+doc.width/2+6, doc.bottomMargin, doc.width/2-6, doc.height, id='col2') | |
| doc.addPageTemplates([PageTemplate(id='TwoCol',frames=[frame1,frame2]), ]) | |
| return doc | |
| def read_voters(filename): | |
| voters = [json.loads(line) for line in open(filename)] | |
| return sorted(voters, key=lambda v: v['name']) | |
| def read_data(filename): | |
| yield ['Name', 'Relation Name', 'Age', 'Voter ID', 'Part', '#'] | |
| for voter in read_voters(filename): | |
| yield [ | |
| voter['name'], | |
| voter['rel_name'], | |
| "{}/{}".format(voter['sex'], voter['age']), | |
| voter['voterid'], | |
| voter['part'], | |
| voter['serial']] | |
| def get_table_style(data): | |
| """prepare table styles. | |
| We want all the rows with female voters with a different background | |
| so that it is easy to spot. | |
| """ | |
| styles = [ | |
| ('LINEBELOW', (0,0), (-1,-1), .5, colors.Color(.3, .3, .3)), | |
| ('BOX',(0,0),(-1,-1),1.0,(0,0,0)), | |
| ('ALIGN', (4, 0), (5, -1), 'RIGHT'), | |
| ('BACKGROUND',(0,0),(-1, 0), colors.black), | |
| ('TEXTCOLOR',(0,0),(-1, 0), colors.white), | |
| ] | |
| for i, row in enumerate(data): | |
| if row[2].startswith("F"): | |
| styles.append(('BACKGROUND', (0, i), (-1, i), colors.Color(.8, .8, .8))) | |
| return TableStyle(styles) | |
| Title = "Hello world" | |
| pageinfo = "platypus example" | |
| def myFirstPage(canvas, doc): | |
| canvas.saveState() | |
| canvas.setFont('Times-Bold',16) | |
| canvas.drawCentredString(doc.width/2.0, doc.height-108, Title) | |
| canvas.setFont('Times-Roman',9) | |
| canvas.drawString(inch, 0.75 * inch, "First Page / %s" % pageinfo) | |
| canvas.restoreState() | |
| def myLaterPages(canvas, doc): | |
| canvas.saveState() | |
| canvas.setFont('Times-Roman',9) | |
| canvas.drawString(inch, 0.75 * inch, "Page %d %s" % (doc.page, pageinfo)) | |
| canvas.restoreState() | |
| def main(): | |
| filename = sys.argv[1] | |
| data = list(read_data(filename)) | |
| t=Table(data, | |
| repeatRows=1, | |
| colWidths=[1.55*inch, 1.55*inch, 0.4*inch, 1.0*inch, 0.35*inch, 0.35*inch], | |
| ) | |
| t.setStyle(get_table_style(data)) | |
| doc = make_doc("voterlist.pdf") | |
| doc.build([t]) | |
| print "generated voterlist.pdf" | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment