[reportlab-users] Vertical text in table cell

Arkadi Colson arkadi at smartbit.be
Tue Feb 1 08:00:22 EST 2011


Is there a way to detect the height of the paragraph? For example when
drawing the text over 2 lines it is needed for alignment...

On 01/31/2011 05:14 PM, Robin Becker wrote:

> #!/usr/bin/env python

>

> """Test for creating vertical table cells in ReportLab tables.

> """

>

> import sys, os.path

>

> from reportlab.lib.units import cm

> from reportlab.lib import colors

> from reportlab.lib.styles import getSampleStyleSheet

> from reportlab.platypus.flowables import Flowable

> from reportlab.platypus import SimpleDocTemplate, Paragraph, Table

>

>

> class RotededText(Flowable):

> '''Rotates a text in a table cell.'''

>

> def __init__(self, text ):

> Flowable.__init__(self)

> self.text=text

>

> def draw(self):

> canvas = self.canv

> canvas.rotate(90)

> fs = canvas._fontsize

> canvas.translate(1, -fs/1.2) # canvas._leading?

> canvas.drawString(0, 0, self.text)

>

> def wrap(self, aW, aH):

> canv = self.canv

> fn, fs = canv._fontname, canv._fontsize

> return canv._leading, 1 + canv.stringWidth(self.text, fn, fs)

>

> class RotatedParagraph(Flowable):

> '''Rotates a text in a table cell.'''

>

> def __init__(self, text ):

> Flowable.__init__(self)

> self.text=text

>

> def draw(self):

> canvas = self.canv

> canvas.rotate(90)

> fs = canvas._fontsize

> canvas.translate(1, -self.height) # canvas._leading?

> self.text.canv = canvas

> try:

> self.text.draw()

> finally:

> del self.text.canv

>

>

> def wrap(self, aW, aH):

> w, h = self.text.wrap(aH,aW)

> self.width,self.height = h, w

> return h, w

>

>

> def main():

> stylesheet = getSampleStyleSheet()

> normal = stylesheet['Normal']

> doc = SimpleDocTemplate("test_rotated_cells.pdf")

> cli = RotatedParagraph(Paragraph('Clientes de cobranca muito duvidosa', style=normal))

>

> data = [

> ('codigo', RotededText('NIVEL'), RotededText('FOOBAR'), 'descricao'),

> ('11', '0', '1', 'Caixa'),

> ('21', '1', '2', cli)

> ]

>

> style = [

> ('ALIGN', (1,0), (1, -1), 'CENTER'),

> ('INNERGRID', (0,0), (-1,-1), 0.25, colors.red),

> ('BOX', (0,0), (-1,-1), 0.25, colors.blue),

> ('VALIGN', (0,0), (-1,-1), 'BOTTOM')

> ]

>

> tab = Table(data, rowHeights=[None,None,100],colWidths=[40, 20, 20, None], style=style)

> story = [tab]

> doc.build(story)

>

>

> if __name__ == "__main__":

> main()


--
Smartbit bvba
Hoogstraat 13
B-3670 Meeuwen
T: +32 11 64 08 80
F: +32 89 46 81 10
W: http://www.smartbit.be
E: arkadi at smartbit.be



More information about the reportlab-users mailing list