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

时间:2015-06-11 09:52:31

标签: python reportlab

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

当前输出:

enter image description here

预期产出:

enter image description here

因此数据应显示在列中,而不是行。

这是我的代码:

# -*- 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 个答案:

答案 0 :(得分:2)

您可以转置数据:

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

这将符合您的预期输出:

enter image description here

答案 1 :(得分:1)

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

data = zip(*data)