python - ReportLab制作简单表格的方法

标签 python pdf pdf-generation

如何在 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/

相关文章:

python - 您将什么称为允许持久操作的非持久数据结构?

python - 以编程方式将图像保存到 Django ImageField

python - 模板的 Django/SQL 复式表

带条件的 Python for 循环

html - 如何嵌入 PDF?

.net - 我们可以使用 wkhtmltopdf.exe 为 pdf 设置显示布局吗

php - PDF 生成在 Chrome 中导致 ERR_INVALID_RESPONSE

ios - vfr 阅读器 2 页横向

pdf-generation - Apache FOP : zindex does not appear to be working in overlapping blocks

javascript - 在 AngularJS 中使用 DocRaptor 生成 PDF