Tag Archives: Reportlab

Reportlab – How to Add Charts / Graphs

Reportlab is really nice tool for creating PDFs in Python. A little known fact is that they now support adding charts or graphs to your PDF. Previously if you wanted that functionality, you would have to do all the drawing code yourself. Unfortunately, the Reportlab guide doesn’t really explain how to use their charts, what kinds of charts are supported or what their parameters / attributes / etc are. They do have some example code snippets on their website though. In this article, we’ll go over a couple of simple examples to show you how to use Reportlab’s charting capabilities.

Continue reading Reportlab – How to Add Charts / Graphs

Reportlab for Python 3 is here!

Reportlab recently released version 3.1 which now fully supports Python 3 and Python 2.7. They had actually released a Python 3 compatible version about a month or so ago, but this one sounds like they’ve worked the bugs out of that initial release as this version also supports their commercial customers. I find this exciting in that one of my favorite Python packages finally supports Python 3. I haven’t moved to Python 3 myself because I use so many packages that are only available for Python 2 (and also because I have yet to work for a place that uses Python 3). There aren’t really any awesome new features in Reportlab 3.1 as of yet, but you can read through their release notes and decide for yourself.

One cool new feature of Reportlab is that you can now install it with pip or easy_install. They have also introduced Python wheel packages as their primary installation type, although you can still download the source. See PyPI for the open source downloads.

Check it out and let me know what you think!

Reportlab: How to Create Custom Flowables

Reportlab is a very flexible PDF creation package for Python. You can layout your documents using absolute positioning or by using Flowable objects, such as a Paragraph, a Table or Frame. You can even mix the two together! In this article, we will be looking at how to create some custom Flowables. For example, what do you do if you need to add a line to demarcate the start of a new section in your document? There isn’t really a built-in Flowable for that, so we’ll design our own. We will also design a flowable that has a line and a box with text inside of it.

Let’s get started! Continue reading Reportlab: How to Create Custom Flowables

Reportlab: How to Create Landscape Pages

The other day I had an interesting task I needed to complete with Reportlab. I needed to create a PDF in landscape orientation that had to be rotated 90 degrees when I saved it. To make it easier to lay out the document, I created a class with a flag that allows me to save it in landscape orientation or flip it into portrait. In this article, we’ll take a look at my code to see what it takes. If you’d like to follow along, I would recommend downloading a copy of Reportlab and pyPdf (or pyPdf2). Continue reading Reportlab: How to Create Landscape Pages

Reportlab: How to Add Page Numbers

Have you ever needed to add page numbers to your Reportlab generated PDF but didn’t know how? Well you’ve come to the right place. We’re going to look at how to add page number in three different circumstances:

  1. How to add page numbers with just the canvas object
  2. How to add page numbers using the SimpleDocTemplate
  3. How to add “Page # of #” (i.e. Page 1 of 10)

Are you ready? Let’s do this thing! Continue reading Reportlab: How to Add Page Numbers

Reportlab: How to Combine Static Content and Multipage Tables

This week I was trying to figure out how to make Reportlab do something I had never attempted before. Namely, I wanted to create about a half page’s worth of static text positioned exactly and then have a table of line items that could potentially fill the rest of the page and continue for N pages thereafter. The problem is that mixing Reportlab’s canvas object with flowables can be messy. Reportlab talks a little about using templates in its user guide, but it only shows how to add header and footer type information. That’s actually all the information I needed, but it took me quite a while to realize that. I asked about how to do this sort of thing on the Reportlab mailing list. At the time of this writing, no one on there told me how to do it. Anyway, I figured it out on my own and now I’m going to show you! If you’d like to follow along, you’ll probably need to go get a free copy of Reportlab yourself. Continue reading Reportlab: How to Combine Static Content and Multipage Tables

Reportlab – All About Fonts

Have you ever wondered how to embed custom fonts in Reportlab? Or maybe you just want to switch fonts or change the font’s color. Well in this tutorial, we’ll take a look at all of these questions. You’ll need to go out and download a copy of Reportlab as it isn’t a part of the standard Python installation. You can go to PyPI or the official Reportlab website to get the package. Reportlab only works with Python 2.5-2.7, so keep that in mind. Once you’re ready, we can continue. Continue reading Reportlab – All About Fonts

Reportlab – How to Create Barcodes in Your PDFs with Python

The Reportlab library is a great way to generate PDFs in Python. Recently, I noticed that it has the ability to do barcodes. I had heard about it being able to generate QR codes, but I hadn’t really dug under the covers to see what else it could do. In this tutorial, we’ll take a look at some of the barcodes that Reportlab can generate. If you don’t already have Reportlab, go to their website and get it before jumping into the article. Continue reading Reportlab – How to Create Barcodes in Your PDFs with Python

Reportlab: Mixing Fixed Content and Flowables

Recently I needed the ability to use Reportlab’s flowables, but place them in fixed locations. Some of you are probably wondering why I would want to do that. The nice thing about flowables, like the Paragraph, is that they’re easily styled. If I could bold something or center something AND put it in a fixed location, then that would rock! It took a lot of Googling and trial and error, but I finally got a decent template put together that I could use for mailings. In this article, I’m going to show you how to do this too. Continue reading Reportlab: Mixing Fixed Content and Flowables

Reportlab: Converting Hundreds of Images Into PDFs

I was recently asked to convert a few hundred images into PDF pages. A friend of mine draws comics and my brother wanted to be able to read them on a tablet. Alas, if you had a bunch of files named something like this:


'Jia_01.Jpg', 'Jia_02.Jpg', 'Jia_09.Jpg', 'Jia_10.Jpg', 'Jia_11.Jpg', 'Jia_101.Jpg'

the Android tablet would reorder them into something like this:


'Jia_01.Jpg', 'Jia_02.Jpg', 'Jia_09.Jpg', 'Jia_10.Jpg', 'Jia_101.Jpg', 'Jia_11.Jpg'

And it got pretty confusing the more files you had that were out of order. Sadly, even Python sorts files this way. I tried using the glob module on the directly and then sorting the result and got the exact same issue. So the first thing I had to do was find some kind of sorting algorithm that could sort them correctly. It should be noted that Windows 7 can sort the files correctly in its file system, even though Python cannot. Continue reading Reportlab: Converting Hundreds of Images Into PDFs