GreenSaber
GreenSaber

Reputation: 1148

Aligning Data in a ReportLab Table

I would like to align data in a ReportLab table so that half the table is aligned to the left, and half the table is aligned to the right. This table is made up of paragraphs and variables. Here is the code:

table_data = []
quote_title = Paragraph(qn, styles['Heading1'])
table_data.append([ttab_empty, quote_title, ttab_empty]) #tab_empy are empty strings
title_table = Table(table_data, colWidths=[5 * cm, 5 * cm, 5 * cm])
title_table.setStyle(TableStyle([('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
    ('BOX', (0, 0), (-1, -1), 0.25, colors.black),
    ('ALIGN', (0, 0), (-1, -1), "CENTER")]))
elements.append(title_table)

However, the align tag isn't aligning the text in the table. Same goes if the align is set to left. How can I align my cell data to the center?

Thanks

Upvotes: 2

Views: 5022

Answers (1)

skidzo
skidzo

Reputation: 415

Is it possible, that your Paragraph overrides the alignment set by TableStyle? Note that you use your styles['Heading1'] which has its own alignment...

a Paragraph inherits reportlab.platypus.Flowable and will draw itself at build time.

hint: see the implementation of the Table in reportlab.platypus.tables.py

some more hints:

use alignment symbols:

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

and self defined font names:

_baseFontName  ='Helvetica'
_baseFontNameB ='Helvetica-Bold'
_baseFontNameI ='Helvetica-Oblique'
_baseFontNameBI='Helvetica-BoldOblique'

try using:

sty= ParagraphStyle(name='Heading1',
                    parent=self.stylesheet['Normal'],
                    fontName = _baseFontNameB,
                    fontSize=18,
                    leading=22,
                    spaceAfter=6,
                    alignment=TA_CENTER)
                    alias='h1')

it is good practice to use Paragraphs and ParagraphStyle within a table. You can calculate the width using the pdfmetrics module:

from reportlab.pdfbase.pdfmetrics import stringWidth, getFont

or by the built in functions:

para=Paragraph(text,sty)

para.minWidth()
print(para.__repr__())

all Flowable should have a function called minWidth(), that's why you could use:

if isinstance(obj,Flowable):
    return obj.minWidth()

finally::

from reportlab.lib.enums import TA_JUSTIFY,TA_LEFT,TA_CENTER,TA_RIGHT
from reportlab.pdfbase.pdfmetrics import stringWidth, getFont

_baseFontName  ='Helvetica'
_baseFontNameB ='Helvetica-Bold'
_baseFontNameI ='Helvetica-Oblique'
_baseFontNameBI='Helvetica-BoldOblique'

from reportlab.platypus import Paragraph, Table, TableStyle, SimpleDocTemplate

from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors
from reportlab.lib.units import inch, cm, mm

styles = getSampleStyleSheet()
qn = "some title"

doc = SimpleDocTemplate("test.pdf")

elements = []

ttab_empty = ""
table_data = []


print(vars(styles['Heading1']))

# that's why you should use your own paragraph style:
sty = ParagraphStyle(name='Heading1',
                     parent=styles['Normal'],
                     fontName = _baseFontNameB,
                     fontSize=18,
                     leading=22,
                     spaceAfter=6,
                     alignment=TA_CENTER)


quote_title = Paragraph(qn, sty)

table_data.append([ttab_empty, quote_title, ttab_empty]) #tab_empy are empty strings
title_table = Table(table_data, colWidths=[5 * cm, 5 * cm, 5 * cm])
title_table.setStyle(TableStyle([('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
    ('BOX', (0, 0), (-1, -1), 0.25, colors.black),
    ('ALIGN', (0, 0), (-1, -1), "CENTER")]))
elements.append(title_table)

doc.multiBuild(elements)

Upvotes: 2

Related Questions