Home arrow Python arrow Page 3 - Python for PDF Generation

Text Formatting Techniques - Python

The Portable Document Format (PDF) lets you create documents that look exactly the same on every platform. Sometimes a PDF document needs to be generated dynamically, however, and that can be quite a challenge. Fortunately, there are libraries that can help. This article examines one of those for Python.

  1. Python for PDF Generation
  2. Putting Virtual Ink to Virtual Paper
  3. Text Formatting Techniques
  4. Using Graphics
By: Peyton McCullough
Rating: starstarstarstarstar / 64
March 14, 2006

print this article



Drawing strings of text one at a time is fine and good for some purposes, like above when we only needed to place two distinct lines of text. However, imagine creating a bigger document. Positioning individual strings of text would get real boring real fast. Thankfully, there are other ways to position text.

Text objects allow for larger amounts of text to be added to a page, starting from a given point. You can pass any number of lines into one, and it will put space in between them. To create a text object, the beginText method must be called, which accepts the starting coordinates and returns a text object:

>>> rhyme = pdf.beginText(inch * 1, inch * 10)

Calling the textLine method allows a line of text to be added. Further calls will each be put on a new line:

>>> rhyme.textLine("Humpty Dumpty sat on a wall.")
>>> rhyme.textLine("Humpty Dumpty had a great fall.")
>>> rhyme.textLine("All the king's horses and all the king's
>>> rhyme.textLine("Couldn't put Humpty together again.")

After all of the necessary text is added, the text object must be drawn to the page:

>>> pdf.drawText(rhyme)

Saving and viewing the page will show that it has been formatted nicely:

>>> pdf.showPage()
>>> pdf.save()

It's also possible to draw all of the lines in the same method call by using the textLines method. The following code will produce a result identical to the previous page, but it requires less work than the previous approach:

>>> rhyme = pdf.beginText(inch * 1, inch * 10)
>>> rhyme.textLines("""Humpty Dumpty sat on a wall.
Humpty Dumpty had a great fall.
All the king's horses and all the king's men
Couldn't put Humpty together again.""")
>>> pdf.drawText(rhyme)
>>> pdf.showPage()
>>> pdf.save()

However, this approach still has its problems. Consider long documents that need to have standard headers, standard footers and wrapped words. Using text objects to do everything isn't entirely practical for documents like this. A tool called Platypus (Page Layout and Typography Using Scripts) exists which can make everything a lot easier. Generating a very simple PDF document with Platypus isn't difficult, either. We'll use simplified tools to get introduced to Platypus. The first step is to import the required modules:

>>> from reportlab.platypus import Paragraph, SimpleDocTemplate,
>>> from reportlab.lib.styles import getSampleStyleSheet

Now that we've imported what we need, it's necessary to call getSampleStyleSheet to get what its name implies--a simple style sheet that we can use:

>>> style = getSampleStyleSheet()

Next, we create an instance of SimpleDocTemplate, which will be used to structure our document:

>>> pdf = SimpleDocTemplate("testplatypus.pdf", pagesize =

Just as in creating a canvas, you're required to pass a filename, which may be either absolute or relative to the current working directory.

We now have to create what's called a story. A story is basically a list of elements (which are termed flowables by Platypus) to be used within a page:

>>> story = []

We'll also need some text for a paragraph (which we will repeat multiple times for an example):

>>> text = "Paragraphs are quite easy to create with Platypus, and Platypus handles things like word wrapping for you. There's not a lot of coding work involved if you wish to create something simple."

We'll loop through and create twenty-five paragraphs for our document, with a half-inch space after each one. All of this will need to be added to our Platypus story:

>>> for x in xrange(25):
      para = Paragraph(text, style["Normal"])
      story.append(Spacer(inch * .5, inch * .5))

Finally, we can generate the resulting document:

>>> pdf.build(story)

Another interesting thing about Platypus it the ability to embed formatting tags inside of paragraphs. It works like this:

>>> pdf = SimpleDocTemplate("testplatypus.pdf", pagesize = letter)
>>> story = []
>>> for color in ["red", "green", "blue"]:
      para = Paragraph("<font color='%s'>This is <b>%
s</b>.</font>" % (color, color), style["Normal"])
      story.append(Spacer(inch * .5, inch * .5))     

>>> pdf.build(story)

The result is three paragraphs, each half an inch apart and in a different color.

Platypus provides a huge benefit over the pdfgen module when it comes to quickly and efficiently generating dynamic documents. Consider this script, which takes the contents of a simple text file and generates a PDF document:

import sys

from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch
from reportlab.platypus import Paragraph, SimpleDocTemplate,

if len(sys.argv) < 3:
    print "Usage: <script> textfile pdffile"
    pdf = SimpleDocTemplate(sys.argv[2], pagesize = letter)
    story = []
    style = getSampleStyleSheet()
    text = file(sys.argv[1]).read()
    paragraphs = text.split("\n")
    for para in paragraphs:
        story.append(Paragraph(para, style["Normal"]))
        story.append(Spacer(0, inch * .1))

The script is used like this:

$ python texttopdf.py textDocument.txt pdfToCreate.txt

>>> More Python Articles          >>> More By Peyton McCullough

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates


Dev Shed Tutorial Topics: