如何在ReportLab中创建一个简单的表

Pol*_*Pol 8 python pdf pdf-generation

如何在ReportLab中创建简单的表?我需要制作一个简单的2x20表并输入一些数据.有人能指点我一个例子吗?

Pol*_*Pol 13

最简单的表函数:

table = Table(data, colWidths=270, rowHeights=79)
Run Code Online (Sandbox Code Playgroud)

多少列和结束行取决于数据元组.我们所有的表函数看起来像:

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
Run Code Online (Sandbox Code Playgroud)

这将生成表2X2,并用数字1,2,3,4填充它.然后你可以制作文件文件.在我的情况下,我做了HttpResponse与文件非常相似.