6

我正在与报告实验室合作,我无法使用鸭嘴兽添加 png 图像这是来自这里的一些示例代码http://www.tylerlesmann.com/2009/jan/28/writing-pdfs-python- added-images/,附加该 png 的错误

你能帮我让它工作吗?

#!/usr/bin/env python

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)



---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/media/Felipe/B1wt/<ipython-input-21-1c3c466b9184> in <module>()
     22 parts = []
     23 parts.append(Image(filename))
---> 24 doc.build(parts)
     25 

/usr/lib/python2.7/site-packages/reportlab/platypus/doctemplate.pyc in build(self, flowables, onFirstPage, onLaterPages, canvasmaker)
   1115         if onLaterPages is _doNothing and hasattr(self,'onLaterPages'):
   1116             self.pageTemplates[1].beforeDrawPage = self.onLaterPages
-> 1117         BaseDocTemplate.build(self,flowables, canvasmaker=canvasmaker)
   1118 
   1119 def progressCB(typ, value):

/usr/lib/python2.7/site-packages/reportlab/platypus/doctemplate.pyc in build(self, flowables, filename, canvasmaker)
    878                 try:
    879                     first = flowables[0]
--> 880                     self.handle_flowable(flowables)
    881                     handled += 1
    882                 except:

/usr/lib/python2.7/site-packages/reportlab/platypus/doctemplate.pyc in handle_flowable(self, flowables)
    761             canv = self.canv
    762             #try to fit it then draw it

--> 763             if frame.add(f, canv, trySplit=self.allowSplitting):
    764                 if not isinstance(f,FrameActionFlowable):
    765                     self._curPageFlowableCount += 1

/usr/lib/python2.7/site-packages/reportlab/platypus/frames.pyc in _add(self, flowable, canv, trySplit)
    157             h = y - p - s
    158             if h>0:
--> 159                 w, h = flowable.wrap(aW, h)
    160             else:
    161                 return 0

/usr/lib/python2.7/site-packages/reportlab/platypus/flowables.pyc in wrap(self, availWidth, availHeight)
    406     def wrap(self, availWidth, availHeight):
    407         #the caller may decide it does not fit.

--> 408         return self.drawWidth, self.drawHeight
    409 
    410     def draw(self):

/usr/lib/python2.7/site-packages/reportlab/platypus/flowables.pyc in __getattr__(self, a)
    400             return self._img
    401         elif a in ('drawWidth','drawHeight','imageWidth','imageHeight'):
--> 402             self._setup_inner()
    403             return self.__dict__[a]
    404         raise AttributeError("<Image @ 0x%x>.%s" % (id(self),a))

/usr/lib/python2.7/site-packages/reportlab/platypus/flowables.pyc in _setup_inner(self)
    366         height = self._height
    367         kind = self._kind
--> 368         img = self._img
    369         if img: self.imageWidth, self.imageHeight = img.getSize()
    370         if self._lazy>=2: del self._img

/usr/lib/python2.7/site-packages/reportlab/platypus/flowables.pyc in __getattr__(self, a)
    396         if a=='_img':
    397             from reportlab.lib.utils import ImageReader  #this may raise an error
--> 398             self._img = ImageReader(self._file)
    399             del self._file
    400             return self._img

/usr/lib/python2.7/site-packages/reportlab/lib/utils.pyc in __init__(self, fileName, ident)
    539         self._transparent = None
    540         self._data = None
--> 541         if _isPILImage(fileName):
    542             self._image = fileName
    543             self.fp = getattr(fileName,'fp',None)

/usr/lib/python2.7/site-packages/reportlab/lib/utils.pyc in _isPILImage(im)
    519 def _isPILImage(im):
    520     try:
--> 521         return isinstance(im,Image.Image)
    522     except ImportError:
    523         return 0

AttributeError: 'NoneType' object has no attribute 'Image'
4

1 回答 1

8

您应该安装PIL (Python Imaging Library),例如

pip install PIL

我猜在导入 PIL 失败时,它设置 Image = None。如果reportlab 源易于浏览,我会确认。

编辑:我们开始:

try:
    import Image
    if PIL_WARNINGS: warnOnce('Python Imaging Library not available as package; upgrade   your installation!')
except ImportError, errMsg:
    _checkImportError(errMsg)
    Image = None
于 2012-07-02T00:57:28.257 回答