How can I make simple table in ReportLab? I need to make a simple 2x20 table and put in some data. Can someone point me to an example?
As mentioned, you can use 'VALIGN' to wrap text within the cells, but there is another hack if you want to further control the table elements on the canvas. Now adding two components: rowHeights attribute. _argH for finer editing of the row heights.
ReportLab is a free open-source document creation engine for generating PDF documents and custom vector graphics.
The simplest table function:
table = Table(data, colWidths=270, rowHeights=79)
How many columns & end rows depend from tuple of data. All our table functions looks like:
from reportlab.platypus import SimpleDocTemplate
from reportlab.platypus.tables import Table
cm = 2.54
def print_pdf(modeladmin, request, queryset):
response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=somefilename.pdf'
elements = []
doc = SimpleDocTemplate(response, rightMargin=0, leftMargin=6.5 * cm, topMargin=0.3 * cm, bottomMargin=0)
data=[(1,2),(3,4)]
table = Table(data, colWidths=270, rowHeights=79)
elements.append(table)
doc.build(elements)
return response
This will make table 2X2, and fill it with numbers 1,2,3,4. Then you can make file document. In my case i made HttpResponse what is pretty the same like file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With