printing from Word using win32com

Robert Amesz sheershion at mailexpire.com
Wed Jan 2 20:10:42 EST 2002


Frederick H. Bartlett wrote:

> But item.encode('latin-1') gives me
>   AttributeError: <unknown>.encode
> so I must not have a COM string. But if it's not a COM string,
> what could it be? Python's print will work so long as the object
> contains only 7-bit characters.


Why then don't you use str(item).encode('latin-1')? The print statment 
uses str() or repr() to convert non-strings to strings. Or does str() 
itself raise an exception?

In that case, use a property from item which *does* yield a proper 
unicode string. For Excel, I used the .text property for Cell-objects 
which I needed to read. For Word, you'd expect to be able to use 
something similar.

Secondly, depending on the locale of your Windows version, you might 
want to use the proper character set for that locale, e.g.:

   encode("CP1252", 'replace')

Note that CP1252 is the native Windows character set, which is a 
superset of LATIN-1 (aka ISO-8859-1). LATIN 1 has a hole in the range 
128-159, but CP1252 does have some characters mapped for that range.

By using 'replace' you can make sure no exceptions will be raised, but 
any characters outside the CP1252 will be replaced by a valid character 
from the set, typically a '?'.


HTH, Robert Amesz



More information about the Python-list mailing list