gpt4 book ai didi

python - 在 AppEngine Python 上使用 Reportlab 生成的 PDF 文档中添加图像文件的正确方法

转载 作者:行者123 更新时间:2023-11-28 19:40:33 25 4
gpt4 key购买 nike

我正在尝试在 App Engine Python 上使用 reportlab 生成 PDF 报告。

但我不知道如何正确附加图像。

图像是静态的。

这是我项目的目录树。

enter image description here

这就是我所做的(在“chipas.py”中)获取图像:

im = Image('../static/logo.png',1*inch, 1*inch)
story.append(im)
...

我得到的堆栈跟踪:

Traceback (most recent call last): File "C:\Users\Lucas\Dropbox\Desarrollo\Python\windows\AppEngine\google\appengine\ext\webapp_webapp25.py", line 701, in call handler.get(*groups) File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\chipas.py", line 1035, in get doc.build(story) File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\reportlab\platypus\doctemplate.py", line 1117, in build BaseDocTemplate.build(self,flowables, canvasmaker=canvasmaker) File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\reportlab\platypus\doctemplate.py", line 880, in build self.handle_flowable(flowables) File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\reportlab\platypus\doctemplate.py", line 763, in handle_flowable if frame.add(f, canv, trySplit=self.allowSplitting): File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\reportlab\platypus\frames.py", line 159, in _add w, h = flowable.wrap(aW, h) File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\reportlab\platypus\flowables.py", line 408, in wrap return self.drawWidth, self.drawHeight File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\reportlab\platypus\flowables.py", line 402, in getattr self._setup_inner() File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\reportlab\platypus\flowables.py", line 368, in _setup_inner img = self._img File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\reportlab\platypus\flowables.py", line 398, in getattr self._img = ImageReader(self._file) File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\reportlab\lib\utils.py", line 541, in init if _isPILImage(fileName): File "C:\Users\Lucas\Dropbox\Desarrollo\workspace\python\chipas-windows\src\reportlab\lib\utils.py", line 521, in _isPILImage return isinstance(im,Image.Image) AttributeError: 'NoneType' object has no attribute 'Image' INFO 2012-02-29 19:54:37,276 dev_appserver.py:4247] "GET /pdf?pedido=ahVkZXZ-Y2hpcGFzLWludGhlY2xvdWRyLwsSBlBlZGlkbyIjMjAxMi0wMi0yOSAxOTo1NDoxOHRlc3RAZXhhbXBsZS5jb20M HTTP/1.1" 500 -

在使用 reportlab 生成的 pdf 文档中添加图像文件的正确方法是什么?非常感谢!

最佳答案

有两种方法可以做到这一点。

如果您只是使用 Canvas :

canvas.drawInlineImage(APP_ROOT + "/static/footer_image.png", inch*.25, inch*.25, PAGE_WIDTH-(.5*inch), (.316*inch))

或者如果您使用的是鸭嘴兽和可流动物

(答案取自:http://www.tylerlesmann.com/2009/jan/28/writing-pdfs-python-adding-images/)

import os
import urllib2
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Image

filename = './python-logo.png'

def get_python_image():
""" Get a python logo image for this example """
if not os.path.exists(filename) :
response = urllib2.urlopen('http://www.python.org/community/logos/python-logo.png')
f = open(filename, 'w')
f.write(response.read())
f.close()

get_python_image()

doc = SimpleDocTemplate("image.pdf", pagesize=letter)
parts = []
parts.append(Image(filename))
doc.build(parts)

关于python - 在 AppEngine Python 上使用 Reportlab 生成的 PDF 文档中添加图像文件的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9505098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com