使用 platypus 生成简单的 pdf 报告
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将得到
'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 thenstyles = getSampleStyleSheet()
.Or you can create your own stylesheet - look through the reportlab's docs on how to do that :)