[reportlab-users] Puzzling Error

Jerome Alet reportlab-users@reportlab.com
Tue, 5 Nov 2002 18:08:49 +0100


On Tue, Nov 05, 2002 at 04:55:51PM -0000, Andy Robinson wrote:
> > exceptions.AttributeError
> > 'unicode' object has no attribute 'write'
> > <traceback object at 0x0328A4D0>
> 
> My guess is you are passing a unicode string as a filename
> and not an 8-bit python string.  Currently we cannot easily test
> for unicode as we have to run on pre-Unicode versions of
> Python.  
> 
> The relevant code is in pdfdoc, class PDFDocument, method saveToFile
> and does this!
> 
>  if type(filename) is StringType:
>             myfile = 1
>             f = open(filename, "wb")
>         else:
>             myfile = 0
>             f = filename # IT BETTER BE A FILE-LIKE OBJECT!
> 
> So if you pass a Unicode string it assumes it's something
> writable.
> 
> Solution:  either do 
>    filename = str(filename)
> in your code before setting up the canvas, or explicitly pass
> in an open file object.

Alternatively pdfdoc.py could be modified in a way similar to :

        if hasattr(filename, "isupper") and callable(filename.isupper) :
            myfile = 1
            f = open(str(filename), "wb")
        else :
            myfile = 0
            f = filename

this is untested but should work

but there's a similar test in canvas.py in Canvas class's save()
method so you'll have to modify it there too.

hth

Jérôme Alet