如何在 ReportLab 中制作简单的表格?我需要制作一个简单的 2x20 表格并放入一些数据。有人能给我举个例子吗?
最佳答案
最简单的表函数:
table = Table(data, colWidths=270, rowHeights=79)
有多少列和结束行取决于数据元组。我们所有的表函数如下所示:
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
这将制作 2X2 表格,并用数字 1、2、3、4 填充它。然后就可以制作文件文档了。在我的例子中,我制作了 HttpResponse,它与文件非常相似。
关于python - ReportLab制作简单表格的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3372885/