如何在ReportLab中制作一个简单的表格

发布于 2024-09-12 07:58:33 字数 65 浏览 7 评论 0原文

如何在ReportLab中制作简单的表格?我需要制作一个简单的 2x20 表并放入一些数据。有人可以给我举个例子吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

━╋う一瞬間旳綻放 2024-09-19 07:58:33

最简单的表格函数:

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 与文件非常相似。

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.

左岸枫 2024-09-19 07:58:33

只是对 radtek 和 Pol 的答案的补充:

您可以用像 io.BytesIO() 这样的缓冲区对象替换 SimpleDocTemplate() 的 response 参数,如下所示:

from reportlab.platypus import SimpleDocTemplate
from reportlab.platypus.tables import Table
import io

cm = 2.54
buffer = io.BytesIO()
doc = SimpleDocTemplate(buffer, rightMargin=0, leftMargin=6.5 * cm, topMargin=0.3 * cm, bottomMargin=0)
... # To be continued below

这在您想要的情况下可能很有用将 PDF 对象转换为字节,然后转换为字节字符串以 JSON 格式发送:

... # Continuation from above code
buffer.seek(0)
buffer_decoded = io.TextIOWrapper(buffer, encoding='utf-8', errors='ignore').read()

return JsonResponse({
    "pdf_bytes": buffer_decoded,
})

取自文档 (https://www.reportlab.com/docs/reportlab-userguide.pdf):

The required filename can be a string, the name of a file to receive the created PDF document; alternatively it can be an object which has a write method such as aBytesIO or file or socket

Just an add on to radtek and Pol's answer:

You can substitute the response argument to SimpleDocTemplate() with a buffer object like io.BytesIO() like so:

from reportlab.platypus import SimpleDocTemplate
from reportlab.platypus.tables import Table
import io

cm = 2.54
buffer = io.BytesIO()
doc = SimpleDocTemplate(buffer, rightMargin=0, leftMargin=6.5 * cm, topMargin=0.3 * cm, bottomMargin=0)
... # To be continued below

This could be useful in cases when you want to convert the PDF object into bytes and then into byte-string to be sent in JSON format:

... # Continuation from above code
buffer.seek(0)
buffer_decoded = io.TextIOWrapper(buffer, encoding='utf-8', errors='ignore').read()

return JsonResponse({
    "pdf_bytes": buffer_decoded,
})

Taken from the doc (https://www.reportlab.com/docs/reportlab-userguide.pdf):

The required filename can be a string, the name of a file to receive the created PDF document; alternatively it can be an object which has a write method such as aBytesIO or file or socket
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文