[reportlab-users] Switching between three column and full width templates on the same page

Brett Davis brettd43 at gmail.com
Sun Aug 28 22:26:40 EDT 2011


Hi Bill,

Here's a rather fiddly answer for some of your questions. It requires you to
know how many columns you want and to know (or fix) their row heights. Page
breaks might make things interesting too. If these things aren't problems
for you, the code below might help.

from reportlab.lib import pagesizes, colors
from reportlab import platypus

document = platypus.BaseDocTemplate('Multiple tables.pdf',
pagesize = pagesizes.A4)
pageWidth, pageHeight = pagesizes.A4

# we want three columns with a table that wraps across them
tableRows = 7
perRowHeight = 21 # this is fiddly, but it's the best I can do
multiFrameHeight = perRowHeight * (tableRows + 1)

# make some frames
# frame specs are left, bottom (taken from bottom of page), width, height
topFrame = platypus.Frame(40, pageHeight - 100, pageWidth - 40 * 2, 60)
leftFrame = platypus.Frame(40, pageHeight - 90 - multiFrameHeight,
pageWidth / 3 - 40 * 2, multiFrameHeight)
centreFrame = platypus.Frame(pageWidth / 3 + 40, pageHeight - 90 -
multiFrameHeight,
pageWidth / 3 - 40 * 2, multiFrameHeight)
rightFrame = platypus.Frame(2 * pageWidth / 3 + 40, pageHeight - 90 -
multiFrameHeight,
pageWidth / 3 - 40 * 2, multiFrameHeight)
lowerFrame = platypus.Frame(40, pageHeight - 150 - multiFrameHeight,
pageWidth - 40 * 2, 60)

# add frames to the document in a page template
document.addPageTemplates([platypus.PageTemplate(id = 'ColumnMixtures',
frames = [topFrame, leftFrame, centreFrame, rightFrame, lowerFrame],
pagesize = pagesizes.A4)])

# make tables
firstTable = platypus.Table([['Wide table heading'], ['Table contents']],
colWidths = pageWidth - 40 * 2)
tableTwo = [['Narrow table heading']]
[tableTwo.append(['Table content {0}'.format(ref)]) for ref in
range(tableRows * 3)]
secondTable = platypus.Table(tableTwo, repeatRows = 1) # repeat first row on
wrap
thirdTable = platypus.Table([['Heading for another wide table'], ['More
table contents']],
colWidths = pageWidth - 40 * 2)

# define styles
myStyle = platypus.TableStyle([('GRID', (0, 0), (-1, -1), 0.5,
colors.black)])
firstTable.setStyle(myStyle)
secondTable.setStyle(myStyle)
thirdTable.setStyle(myStyle)

# add tables
elements = []
elements.append(firstTable)
elements.append(platypus.FrameBreak())
elements.append(secondTable)
elements.append(platypus.FrameBreak())
elements.append(thirdTable)

document.build(elements)


Regards,
Brett
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20110829/9047b6f7/attachment.html>


More information about the reportlab-users mailing list