This recipe shows how some basics of how to fetch data from database tables managed by the Peewee ORM (a lightweight expressive ORM for Python) and write that data, formatted, to a PDF file. The recipe uses Python, the Peewee ORM and the xtopdf toolkit for PDF creation.
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 75 | # PeeweeToPDF.py
# Purpose: To show basics of publishing Peewee ORM data to PDF.
# Requires: Peewee ORM and xtopdf.
# Author: Vasudev Ram
# Copyright 2016 Vasudev Ram
# Web site: https://vasudevram.github.io
# Blog: http://jugad2.blogspot.com
# Product store: https://gumroad.com/vasudevram
from peewee import *
from PDFWriter import PDFWriter
def print_and_write(pw, s):
print s
pw.writeLine(s)
# Define the database.
db = SqliteDatabase('contacts.db')
# Define the model for contacts.
class Contact(Model):
name = CharField()
age = IntegerField()
skills = CharField()
title = CharField()
class Meta:
database = db
# Connect to the database.
db.connect()
# Drop the Contact table if it exists.
db.drop_tables([Contact])
# Create the Contact table.
db.create_tables([Contact])
# Define some contact rows.
contacts = (
('Albert Einstein', 22, 'Science', 'Physicist'),
('Benjamin Franklin', 32, 'Many', 'Polymath'),
('Samuel Johnson', 42, 'Writing', 'Writer')
)
# Save the contact rows to the contacts table.
for contact in contacts:
c = Contact(name=contact[0], age=contact[1], \
skills=contact[2], title=contact[3])
c.save()
sep = '-' * (20 + 5 + 10 + 15)
# Publish the contact rows to PDF.
with PDFWriter('contacts.pdf') as pw:
pw.setFont('Courier', 12)
pw.setHeader('Demo of publishing Peewee ORM data to PDF')
pw.setFooter('Generated by xtopdf: slides.com/vasudevram/xtopdf')
print_and_write(pw, sep)
print_and_write(pw,
"Name".ljust(20) + "Age".center(5) +
"Skills".ljust(10) + "Title".ljust(15))
print_and_write(pw, sep)
# Loop over all rows queried from the contacts table.
for contact in Contact.select():
print_and_write(pw,
contact.name.ljust(20) +
str(contact.age).center(5) +
contact.skills.ljust(10) +
contact.title.ljust(15))
print_and_write(pw, sep)
# Close the database connection.
db.close()
|
This recipe can be useful (as is, or after customization) when you want to publish database data to PDF (for the benefits that PDF has), and the database is managed by Peewee, a small and expressive ORM for Python.
(You can use this recipe for PDF generation even if the database is not normally managed by Peewee, since the operation is read-only with respect to the database).
Though transformations (other than simple string formatting) are not shown in the recipe, you can easily add any data transformations in the code, after fetching the data from the database, and before writing it to PDF, including powerful transformations enabled by Python features such as lists, dictionaries, sets, list and other comprehensions, lambda functions, etc.