如何在列中显示 table 中的数据,而不是在行中?

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

我正在使用 Reportlab 生成 pdf。如何在 table 中显示列中的数据,而不是行?

当前输出:

预期输出:

所以数据应该显示在列中,而不是行中。

这是我的代码:

# -*- 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()

您可以转置您的数据:

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

这将符合您的预期输出:

这会将您的行转为列,并将列转为行

data = zip(*data)