printing from Word using win32com

Ian Parker parker at gol.com
Wed Jan 2 07:06:28 CET 2002


In article <3C2D2007.AD251D51 at optonline.net>, Frederick H. Bartlett
<fbartlet at optonline.net> writes
>How do I get rid of the UnicodeError in 
>
>    something = myWord.ActiveDocument.Paragraphs
>    for item in something:
>        try:
>            print str(item).encode('latin-1')
>        except UnicodeError:
>            print "XXX There was a Unicode Error."
>
>Thanks!

If you check the actual exception and get the following 

UnicodeError: ASCII decoding error: ordinal not in range(128)

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

#! rnews 2172
Path: nnrp.gol.com!newsfeed.gol.com!newsfeed.mathworks.com!btnet-
peer!btnet!diablo.netcom.net.uk!netcom.net.uk!newsfeeds.belnet.be!news.b
elnet.be!newsfeed1.news.nl.uu.net!sun4nl!C1S5EXCH.Origin-it.com
NNTP-Posting-Host: 172.16.244.177
Newsgroups: comp.lang.python
Date: Mon, 30 Oct 2000 17:18:49 +0100
Message-ID: <iJ39OUT66jm62w7pd+z7KC8F9+1N at 4ax.com>
From: Paul Moore <paul.moore at uk.origin-it.com>
Subject: Re: Using more than 7 bit ASCII on windows.
References: <8tdlgd$r0t$1 at news.nuri.net> <39FA90D9.7070403 at ActiveState.c
om> <8tfpf7$jnn$2 at troll.powertech.no> <c8povssplkbmlune701ps229iu2ohoa05
o at 4ax.com> <39FCA0B5.6040606 at ActiveState.com> <+k=9Obpkexxebyymyh8GKU+y+
3g8 at 4ax.com> <JmL9OY9FJWKYSmZT5wV46STMeZkC at 4ax.com>
<8tjusf01d0 at news1.newsguy.com>
X-Newsreader: Forte Agent 1.6/32.525
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 29
Xref: newsfeed.gol.com comp.lang.python:103458
X-Received-Date: Tue, 31 Oct 2000 01:36:57 JST (nnrp.gol.com)

On Mon, 30 Oct 2000 14:54:24 +0100, "Alex Martelli"
<aleaxit at yahoo.com> wrote:
>print couldn't care less -- it delegates the transform-into-string
>to str.  str applied to a Unicode object, in turn, wants _ASCII_
>encoding, as the message on the UnicodeError is telling you.
>
>os.chdir is apparently also "calling str" (the C API equivalent
>thereof, no doubt, in both cases:-), with similar results.

That makes sense, and (sort of) clears up the confusion. I'm lost in a
mess of conflicting code pages, though (latin1, vs cp437 (OEM), ...)
But that's not a Python problem...

>It sure would be nice to have a way to control the default
>encoding -- not have ASCII hard-wired.  There is a function
>(not in the 2.0 docs -- doc error?), sys.getdefaultencoding(),
>that _tells_ you what the default encoding is (and guess
>which one...:-), but I do not know of a way to *change* it.

Put the following in sitecustomize.py. The problem is that site.py
deletes sys.setdefaultencoding at the end, presumably so that user
code can't change it...

import sys
sys.setdefaultencoding("latin1")

Hope this helps,
Paul. (Happy to be supplying an answer rather than a question!)


-- 
Ian Parker



More information about the Python-list mailing list