I'm working with report lab, I can't get a way to add png image with platypus this is some sample code from here http://www.tylerlesmann.com/2009/jan/28/writing-pdfs-python-adding-images/, with the error for append that png
Could you help me to get it working?
#!/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'
ReportLab is a free open-source document creation engine for generating PDF documents and custom vector graphics.
You should install PIL (Python Imaging Library), with e.g.
pip install PIL
I am guessing that on failure to import PIL, it is setting Image = None. If the reportlab source was easy to browse, I would confirm.
Edit: and here we go:
try:
import Image
if PIL_WARNINGS: warnOnce('Python Imaging Library not available as package; upgrade your installation!')
except ImportError, errMsg:
_checkImportError(errMsg)
Image = None
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With