printing from Word using win32com

Mark Hammond mhammond at skippinet.com.au
Wed Jan 2 01:43:53 EST 2002


Ian Parker wrote:

> In article <3C2D2007.AD251D51 at optonline.net>, Frederick H. Bartlett
> <fbartlet at optonline.net> writes


Oops - I missed the original:

>>       try:
>>           print str(item).encode('latin-1')
>>       except UnicodeError:
>>           print "XXX There was a Unicode Error."


Just:
   print item.encode('latin-1')

should work assuming item is always a COM string (ie, a Unicode object). 
  Note that using "mbcs" may be a better choice depending on your user base.

> you're having the same problem I had with Excel.  Python defaults to
> ASCII data (7bit - 128 characters).  To handle the full 8 bits (256
> characters) you may need "Latin-1".  
> 
> I put the following into sitecustomize.py
> 
> 
> import sys
> encoding = 'latin-1'
> sys.setdefaultencoding(encoding)
> 
> 
> This solved all my problems with handling English language MS software.

> 
> I got this tip from Paul Moore, who explained everything much more
> clearly, see email below


I am not too keen on this solution.  Python chose not to use latin-1 for 
good reason, and while a PITA, I believe that explicit conversions are 
better in the long run.

Mark.




More information about the Python-list mailing list