[reportlab-users] Composing Tables Without Platypus?

Rich Shepard rshepard at appl-ecosys.com
Tue Oct 30 20:42:53 EDT 2007


I've re-read the Users Guide, API, textobject.py, and canvas.py without
seeing how to print tabular data with the fine positioning control offered
by the canvas. My learning script is attached, and I'd greatly appreciate a
nudge in the proper direction.

Also suggestions and recommendations based on your experiences are most
welcome. Eventually -- and Real Soon Now, I hope -- I want to retrieve the
data in table 't' from a database table and print three pages, one for each
set of data, with the pdf file name filed in appropriately for each page.

Rich

--
Richard B. Shepard, Ph.D. | The Environmental Permitting
Applied Ecosystem Services, Inc. | Accelerators(TM)
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
-------------- next part --------------
#!/usr/bin/env python

from reportlab.pdfgen import canvas
from reportlab.platypus.tables import Table, TableStyle, CellStyle, LongTable
from reportlab.lib import colors
from reportlab.lib.units import inch, cm
from reportlab.lib.pagesizes import LETTER, landscape, portrait
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_RIGHT

pairData = [(u'Jobs', u'Tax base'),
(u'Jobs', u'Infrastructure'),
(u'Jobs', u'Schools'),
(u'Jobs', u'Housing'),
(u'Jobs', u'Medical care'),
(u'Jobs', u'Sustainability'),
(u'Jobs', u'Traffic volume'),
(u'Tax base', u'Infrastructure'),
(u'Tax base', u'Schools'),
(u'Tax base', u'Housing'),
(u'Tax base', u'Medical care'),
(u'Tax base', u'Sustainability'),
(u'Tax base', u'Traffic volume'),
(u'Infrastructure', u'Schools'),
(u'Infrastructure', u'Housing'),
(u'Infrastructure', u'Medical care'),
(u'Infrastructure', u'Sustainability'),
(u'Infrastructure', u'Traffic volume'),
(u'Schools', u'Housing'),
(u'Schools', u'Medical care'),
(u'Schools', u'Sustainability'),
(u'Schools', u'Traffic volume'),
(u'Housing', u'Medical care'),
(u'Housing', u'Sustainability'),
(u'Housing', u'Traffic volume'),
(u'Medical care', u'Sustainability'),
(u'Medical care', u'Traffic volume'),
(u'Sustainability', u'Traffic volume')]

ratingData = [('Importance Value', 'Definition'),
('1', 'Equal importance'),
('3', 'Weak importance of one over the other'),
('5', 'Strong importance of one over the other'),
('7', 'Demonstrated importance of one over the other'),
('9', 'Absolute importance of one over the other'),
('2, 4, 6, 8', 'Intermediate values between the two adjacent definitions')]

instructPara = "Using the table below, determine your preference for either the first \
item of the pair (on the left), or the second (on \
the right). Use the rating table to determine the \
strength of your preference, then fully fill in the \
rectangle for that preference strenth on the same \
line as the component pairs. If you prefer the \
second more than the first, also completely fill in \
the last box on the right."

Title = "Economic Components"
width, height = LETTER

def textStyle():
canvas.Canvas.setFont('Times-Roman',9)

def getTable():
t = Table(pairData,hAlign='RIGHT')
return t

def getTable2():
r = Table(ratingData,hAlign='CENTER')
return r

def makeTableStyles():
tStyles = []
tStyles.append(TableStyle([('ALIGN', (0,0), (-1,-1), 'LEFT'),
('FONT', (0,0), (-1,-1), 'Helvetica', 9, 10)]))
return tStyles

def makeScoreStyles():
sStyles = []
sStyles.append(TableStyle([('ALIGN', (0,0), (0,-1), 'CENTER'),
('ALIGN', (1,0), (1,0), 'CENTER'),
('ALIGN', (1,1), (1,-1), 'LEFT'),
('FONT', (0,0), (0,1), 'Times-Roman', 10, 11),
('FONT', (1,0), (-1,-1), 'Helvetica', 9, 10),
('LINEABOVE', (0,0), (-1,0), 1, colors.black),
('LINEBELOW', (0,0), (-1,0), 1, colors.black)]))
return sStyles

def run():
pg = canvas.Canvas('ecoForm.pdf', pagesize=LETTER)
textobject = pg.beginText()
textobject.setFont('Helvetica-Bold', 13)
textobject.setTextOrigin(3*inch,10.5*inch)
textobject.textOut(Title)
textobject.moveCursor(-10,10)
textobject.setFont('Times-Roman', 9)
# print table t here
# textobject.textOut(getTable())
pg.drawText(textobject)
textobject = pg.endText()
pg.showPage()
pg.save()



if __name__ == "__main__":
run()







More information about the reportlab-users mailing list