[reportlab-users] Positioning Paragraphs and Tables Precisely

Rich Shepard rshepard at appl-ecosys.com
Fri Oct 12 17:57:12 EDT 2007


On Fri, 12 Oct 2007, Andy Robinson wrote:


> Currently the width is ignored. We were thinking 'too far ahead' 7

> years ago and envisaged it might be used one day when packing boxes

> horizontally.


Andy,

Rats!


>> For example, to move a paragraph close to the top of the page, can I

>> .append(Spacer (-1.0*inch, 0))?

>

> Never tried it but it's isn't supposed to do that. It's just to create

> extra vertical space between elements, when spaceBefore and spaceAfter on

> paragraphs don't give you enough. the packing algorithm just sees it and

> moves down.


I wrote that incorrectly. It does accept a negative value to move the text
toward the top of the page. Haven't tried spaceBefore and spaceAfter yet.
Just feeling my way along.


> Tables are centered by default but have an hAlign attribute to control

> their overall position (assuming they are narrower than the enclosing

> frame). e.g.

>

> from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_RIGHT

> t = Table(...., hAlign=TA_LEFT)

>

> You can pass it to the constructor, or set it on the table after construction.


I saw the hALIGN mentioned in the docs, but didn't have the syntax
correct. Unfortunately, it's still not working, even after I imported enums
and added the assignment to the table constructor.

The working file is attached (along with data to replace the selection
from the database. The horizontal alignment of the table is still centered.

I suppose that the ugly kludge is to make a table with more columns, then
use only the two left-most ones.

Thanks,

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 pysqlite2 import dbapi2 as sqlite3

from reportlab.platypus import Spacer, SimpleDocTemplate, Table, TableStyle
from reportlab.platypus.paragraph import Paragraph
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
from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_RIGHT

con = sqlite3.connect('burro-breath.db', isolation_level=None)
cur = con.cursor()

cur.execute("SELECT comp1, comp2 from Pairs where pair_cat='Economic'")
EcoPairs = cur.fetchall()

cur.execute("SELECT comp1, comp2 from Pairs where pair_cat='Natural'")
NatPairs = cur.fetchall()

cur.execute("SELECT comp1, comp2 from Pairs where pair_cat='Societal'")
SocPairs = cur.fetchall()


def getHeading():
stylesheet=getSampleStyleSheet()
headingStyle = stylesheet['Heading3']
h = Paragraph("Economic Components", headingStyle)
return h

def getTable():
t = Table(EcoPairs, hAlign=TA_LEFT)
return t

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

def run():
doc = SimpleDocTemplate('ecoForm.pdf', pagesize=(8.5*inch, 11*inch), showBoundary=0)
tStyles = makeTableStyles()
story = []
story.append(Spacer(0,-1*inch))
p = getHeading()
story.append(p)

for style in tStyles:
t = getTable()
t.setStyle(style)
story.append(t)
doc.build(story)

cur.close()
con.close()

if __name__ == "__main__":
run()

"""
[(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')]
"""




More information about the reportlab-users mailing list