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

百般思念 提交于 2020-06-13 08:03:17

问题


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

回答1:


You could transpose your data:

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

That will match your expected output:

enter image description here




回答2:


This transpose your rows to columns and columns to rows

data = zip(*data)


来源:https://stackoverflow.com/questions/30777364/how-can-i-display-data-in-table-in-columns-not-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!