printing from Word using win32com

Ian Parker parker at
Wed Jan 2 01:06:28 EST 2002

In article <3C2D2007.AD251D51 at>, Frederick H. Bartlett
<fbartlet at> 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."

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

import sys
encoding = 'latin-1'

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
Newsgroups: comp.lang.python
Date: Mon, 30 Oct 2000 17:18:49 +0100
Message-ID: <iJ39OUT66jm62w7pd+z7KC8F9+1N at>
From: Paul Moore <paul.moore at>
Subject: Re: Using more than 7 bit ASCII on windows.
References: <8tdlgd$r0t$1 at> <39FA90D9.7070403 at ActiveState.c
om> <8tfpf7$jnn$2 at> <c8povssplkbmlune701ps229iu2ohoa05
o at> <39FCA0B5.6040606 at> <+k=9Obpkexxebyymyh8GKU+y+
3g8 at> <JmL9OY9FJWKYSmZT5wV46STMeZkC at>
<8tjusf01d0 at>
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: comp.lang.python:103458
X-Received-Date: Tue, 31 Oct 2000 01:36:57 JST (

On Mon, 30 Oct 2000 14:54:24 +0100, "Alex Martelli"
<aleaxit at> 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 The problem is that
deletes sys.setdefaultencoding at the end, presumably so that user
code can't change it...

import sys

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

Ian Parker

More information about the Python-list mailing list