Reportlab Insert horizontal line in table

Question:

I know about the LINEABOVE and LINEBELOW styles, i was wondering if there is a way to draw a line in the table with a specified width.

I’m trying to add a line that does not ‘touch’ the border of the table, LINEABOVE would work perfectly if i could add a bit of padding between the cells.

Asked By: CJ4

||

Answers:

You can just draw a line within the contents of the cell using the Graphics module. You can put essentially anything inside a cell and lay it out within the table cell to achieve what you want.

Answered By: G Gordon Worley III

You can use canvas.line as shown below:

from reportlab.platypus import Paragraph, SimpleDocTemplate

class MyTemplate(object):
    def __init__(self, report_data, page_size='A4',
        report_type='Full Report', show_header_info=True):
        self.page_size = A4

    def _header_footer(self, canvas, doc):
        # do some stuff...

    def get_data(self):
        buff = BytesIO()
        doc = SimpleDocTemplate(buff, rightMargin=0.5 * inch,
                                leftMargin=0.5 * inch,
                                topMargin=110, bottomMargin=70)

        self.canv.line(doc.leftMargin, 100, doc.width, 100)
        doc.build(elements, onFirstPage=self._header_footer,
                  onLaterPages=self._header_footer,
                  canvasmaker=TEMPLATE.NumberedCanvas)
        pdf = buff.getvalue()
        buff.close()
        return pdf

The order of parameters are, x1,y1, x2, y2
and the coordinate system has the (0, 0) at the bottom left.

Answered By: max

Another strategy is to make a very thin row and then fill it with
('BACKGROUND', (0,1), (-1,1), colors.black)

Answered By: benjo
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.