How can I display data in table in columns, not row?

1.5k views Asked by At

I'm using Reportlab for pdf generate. How can I display data in table in columns, not row?

Current output:

enter image description here

Expected output:

enter image description here

So data should be displayed in columns, not row.

Here my code:

# -*- coding: utf-8 -*-
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4, landscape
from reportlab.platypus.tables import TableStyle, Table
from reportlab.pdfbase import pdfmetrics
from reportlab.platypus.paragraph import Paragraph
from reportlab.lib import styles
from reportlab.lib import colors

canv = canvas.Canvas('plik.pdf', pagesize=landscape(A4))
width, height = landscape(A4)  # keep for later

canv.setFillColorRGB(0, 0, 0.50)
canv.line(40, height - 60, width - 40, height - 60)

stylesheet = styles.getSampleStyleSheet()
normalStyle = stylesheet['Normal']

P = Paragraph('''<font color=red>brak</font>''', normalStyle)

data = [
    ['1', 'Test1', 'Description1'],
    ['2', 'Test2', 'Description2'],
    ['3', 'Test3', 'Description3'],
]

t = Table(data, rowHeights=35, repeatCols=1)

t.setStyle(TableStyle([
    ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
    ('ALIGN', (-2, 1), (-2, -1), 'RIGHT'),
    ('GRID', (0, 0), (-1, -1), 0.25, colors.black),
    ('BOX', (0, 0), (-1, -1), 0.25, colors.black),
    ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),

]))

t.wrapOn(canv, width - 250, height)
w, h = t.wrap(100, 100)
t.drawOn(canv, 284, height - (h + 90), 0)

canv.showPage()
canv.save()
2

There are 2 answers

0
Padraic Cunningham On BEST ANSWER

You could transpose your data:

t = Table(zip(*data), rowHeights=35, repeatCols=1)

That will match your expected output:

enter image description here

0
Ajay On

This transpose your rows to columns and columns to rows

data = zip(*data)