使用 platypus 生成简单的 pdf 报告

发布于 2024-12-01 04:51:41 字数 806 浏览 1 评论 0原文

我正在尝试使用 django 中的 reportlab 生成 pdf 报告。我可以通过直接使用画布来开始一份简单的报告,但看起来 platypus 应该会让事情变得更容易。但我无法让简单的鸭嘴兽报告发挥作用。

def all_comps_pdf_report(request):

    # Set up HttpResponse object
    response = HttpResponse(mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=all_competencies.pdf'

    from reportlab.platypus.doctemplate import SimpleDocTemplate
    from reportlab.platypus import Paragraph
    from reportlab.lib import styles

    doc = SimpleDocTemplate(response)
    Elements = []
    p = Paragraph("Hello World", styles['Heading1'])
    Elements.append(p)
    doc.build(Elements)
    return response

我收到错误 'module' object is unsubscriptable,它抱怨行 p = Paragraph("Hello World", styles['Heading1'])。我做错了什么?

I am trying to generate a pdf report using reportlab in django. I can get a simple report started by working directly with the canvas, but it looks like platypus should make things easier. But I can't get a simple platypus report to work.

def all_comps_pdf_report(request):

    # Set up HttpResponse object
    response = HttpResponse(mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=all_competencies.pdf'

    from reportlab.platypus.doctemplate import SimpleDocTemplate
    from reportlab.platypus import Paragraph
    from reportlab.lib import styles

    doc = SimpleDocTemplate(response)
    Elements = []
    p = Paragraph("Hello World", styles['Heading1'])
    Elements.append(p)
    doc.build(Elements)
    return response

I am getting an error 'module' object is unsubscriptable, which is complaining about the line p = Paragraph("Hello World", styles['Heading1']). What am I doing wrong?

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

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

发布评论

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

评论(1

瑕疵 2024-12-08 04:51:41

您将得到 'module' object is unsubscriptable 因为您将模块视为数组:)

如果您浏览reportlab的源代码,那么您将看到 styles 只是一个具有以下内容的模块里面有很多东西。

要使此示例正常工作,您需要导入样式表:from reportlab.lib.styles import getSampleStyleSheet,然后 styles = getSampleStyleSheet()

或者您可以创建自己的样式表 - 查看报告实验室的文档以了解如何执行此操作:)

You are getting 'module' object is unsubscriptable because you are treating module as it is an array :)

If you'll browse through the source of reportlab then you'll see that styles is just a module with a lot of stuff in it.

For this example to work, you need to import stylesheets: from reportlab.lib.styles import getSampleStyleSheet and then styles = getSampleStyleSheet().

Or you can create your own stylesheet - look through the reportlab's docs on how to do that :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文