问题
I'm using Reportlab for pdf generate. How can I display data in table in columns, not row?
Current output:
Expected output:
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:
回答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