This recipe shows how to create tabular PDF reports, i.e. PDF reports containing tabular data - rows and columns, using Python, xtopdf (a PDF creation library) and tablib (a Python library for manipulating tabular data).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | """
TablibToPDF.py
Author: Vasudev Ram
Copyright 2014 Vasudev Ram - www.dancingbison.com
This program is a demo of how to use the tablib and xtopdf Python libraries
to generate tabular data reports as PDF output.
Tablib is at: https://tablib.readthedocs.org/en/latest/
xtopdf is at: https://bitbucket.org/vasudevram/xtopdf
and info about xtopdf is at: http://slides.com/vasudevram/xtopdf or
at: http://slid.es/vasudevram/xtopdf
"""
import random
import tablib
from PDFWriter import PDFWriter
# Helper function to output a string to both screen and PDF.
def print_and_write(pw, strng):
print strng
pw.writeLine(strng)
# Set up grade and result names and mappings.
grade_letters = ['F', 'E', 'D', 'C', 'B', 'A']
results = {'A': 'Pass', 'B': 'Pass', 'C': 'Pass',
'D': 'Pass', 'E': 'Pass', 'F': 'Fail'}
# Create an empty Dataset and set its headers.
data = tablib.Dataset()
data.headers = ['ID', 'Name', 'Marks', 'Grade', 'Result']
widths = [5, 12, 8, 8, 12] # Display widths for columns.
# Create some rows of student data and use it to populate the Dataset.
# Columns for each student row correspond to the header columns
# shown above.
for i in range(20):
id = str(i).zfill(2)
name = 'Student-' + id
# Let's grade them on the curve [1].
# This examiner doesn't give anyone 100 marks :)
marks = random.randint(40, 99)
# Compute grade from marks.
grade = grade_letters[(marks - 40) / 10]
result = results[grade]
columns = [id, name, marks, grade, result]
row = [ str(col).center(widths[idx]) for idx, col in enumerate(columns) ]
data.append(row)
# Set up the PDFWriter.
pw = PDFWriter('student_grades.pdf')
pw.setFont('Courier', 10)
pw.setHeader('Student Grades Report - generated by xtopdf')
pw.setFooter('xtopdf: http://slides.com/vasudevram/xtopdf')
# Generate header and data rows as strings; output them to screen and PDF.
separator = '-' * sum(widths)
print_and_write(pw, separator)
# Output headers
header_strs = [ header.center(widths[idx]) for idx, header in enumerate(data.headers) ]
print_and_write(pw, ''.join(header_strs))
print_and_write(pw, separator)
# Output data
for row in data:
print_and_write(pw, ''.join(row))
print_and_write(pw, separator)
pw.close()
# [1] http://en.wikipedia.org/wiki/Grading_on_a_curve
# I'm not endorsing the idea of grading on a curve; I only used it as a
# simple algorithm to generate the marks and grades for this example.
|
Tabular reports are very commonly used in all kinds of computing applications. Tablib has other features that I have not used in this recipe, some of those features can be used to good effect in report generation programs. Examples include sorting the tables in various orders before generating the report, and dynamic columns. See the tablib documentation for details, at:
https://tablib.readthedocs.org/en/latest/
More complete details, including a screenshot of the output, are available at this blog post:
http://jugad2.blogspot.in/2014/12/create-tabular-pdf-reports-with-python.html