【问题标题】:Generate simple pdf report using platypus使用鸭嘴兽生成简单的pdf报告
【发布时间】:2011-08-21 17:49:14
【问题描述】:

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

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']) 行。我做错了什么?

【问题讨论】:

    标签: django pdf reportlab platypus


    【解决方案1】:

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

    如果你浏览reportlab的源代码,你会发现styles只是一个模块,里面有很多东西。

    要使本示例生效,您需要导入样式表:from reportlab.lib.styles import getSampleStyleSheet,然后是 styles = getSampleStyleSheet()

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

    【讨论】:

    • styles['Heading1'] => styles == 模块,['Heading1'] 不允许
    • 为了清楚起见,仅突出显示错误发生的确切点。继续,这里没什么可看的~~ >.>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多