[odf-devel] bug in addPicture
Toon Verstraelen
Toon.Verstraelen at UGent.be
Sat Mar 21 01:52:43 EDT 2009
Hi,
I've been using odfpy-0.8 to generate supporting information for a
scientific paper. These documents typically contain a lot of pictures
and data that one does not want to enter by hand. I ran into a bug when
adding a picture based on a filename:
Traceback (most recent call last):
File "../../bin/supinf.py", line 59, in <module>
href = doc.addPicture(fn)
File "/usr/local//lib/python2.5/site-packages/odf/opendocument.py",
line 290, in addPicture
self.Pictures[manifestfn] = (IS_FILENAME, fileobj, mediatype)
NameError: global name 'fileobj' is not defined
It was easy to fix: fileobj must simply be replaced by filename on line
290. However, I do find the addPicture method very confusing and the
document string talks about file objects that are nowhere in the code. I
suggest to split the method into two different ones, see below. That
makes the code at least understandable.
best regards,
Toon
def addPicture(self, filename, mediatype=None):
""" Add a picture
It uses the same convention as OOo, in that it saves the
picture in
the zipfile in the subdirectory 'Pictures'.
If mediatype is not given, it will be guessed from the
filename
extension.
"""
if mediatype is None:
mediatype, encoding = mimetypes.guess_type(filename)
if mediatype is None:
mediatype = ''
try: ext = filename[filename.rindex('.'):]
except ValueError: ext=''
else:
ext = mimetypes.guess_extension(mediatype)
manifestfn = "Pictures/%0.0f%s" % ((time.time()*10000000000), ext)
self.Pictures[manifestfn] = (IS_FILENAME, filename, mediatype)
return manifestfn
def addPictureString(self, content, mediatype):
""" Add a picture
It uses the same convention as OOo, in that it saves the
picture in
the zipfile in the subdirectory 'Pictures'. The content
variable
is a string that contains the binary image data. The mediatype
indicates the image format.
"""
ext = mimetypes.guess_extension(mediatype)
manifestfn = "Pictures/%0.0f%s" % ((time.time()*10000000000), ext)
self.Pictures[manifestfn] = (IS_IMAGE, content, mediatype)
return manifestfn
More information about the odf-devel
mailing list