[reportlab-users] Problems Printing Dates & Times -- FIXED

Tim Roberts timr at probo.com
Thu Nov 1 19:44:27 EDT 2007


Rich Shepard wrote:

> On Thu, 1 Nov 2007, Rich Shepard wrote:

>

>> I want to print the date and time a report is printed using the python

>> datetime() module. It's not working as expected.

>

>> pg.drawCentredString(306,748,now)

>

>> Python apparently can't calculate the string width:

>

> It wasn't Python, but ReportLab. The latter was expecting a UTF8

> formatted

> string, not an ASCII string.


No, that's not it, although the result is the same. The
datetime.datetime.now() function returns a datetime object, not a
string. A datetime object can be converted to a string, and in many
places where a string is expected, that will happen automatically.

However, ReportLab assumes that if an object is not a Unicode string,
then it it must be something that supports the "decode" method (as
strings do). A datetime object can be converted to a string, but it is
NOT a string and does not have a "decode" method.

Calling str() or strftime() is the right solution.

One might argue that the code in _formatText should read:
if not isinstance(text,unicode):
if not isinstance(text,str):
text = unicode(text)
else:
try:
text = text.decode('utf8')
except UnicodeDecodeError,e:
etc.

However, maybe it's better to just keep the rule that "you must pass
strings to functions that expect strings".

--
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the reportlab-users mailing list