This blog is now hosted at consciou.us

Wednesday, March 26, 2008

ReportLab ImageAndFlowables + drawing

So, I've been doing a lot of work with ReportLab lately, and I discovered something that is under-documented, and something that works, but seems like it might be contra-indicated. Hit the link for a description.

The under-documented bit is that the Drawing class is a Flowable. What this means: you can use all the graphics libraries (including all the chart creation) directly within platypus formatted documents. This is great, and here's why:

I'm trying to create charts to include in my report, and up until figuring this out, I've been using matplotlib, exporting to PNG, and importing into my document as an Image object. This works acceptably, but seems a little balky when the images get scaled.

I went looking for a way to embed vector graphics into my document (no scaling issues!), and happened upon the above. Then I decided to take it to another level entirely, and see if the Drawing could be embedded in an ImageAndFlowable (as the image). In short, the following works:

drawing = Drawing(400, 200)
bc = VerticalBarChart()
drawing.add(bc)
p = Paragraph("spam " * 200, style['Normal'])
fi = ImageAndFlowables(drawing, p)
Story.append(fi)


Which happens to be exactly what I needed, and I'm psyched.

1 comment:

Anonymous said...

can you provide a bit more about the scripts you wrote about this? thanks. Evan