Unicode, COM, Word Problem

Kirby James kirby at jamesk.freeserve.co.uk
Sat Jan 20 03:54:28 EST 2001


Hi, I'm writing a script to read and summarize a large number of MS Word-97
documents, using the Python win32com.client. I open each file in turn (using
COM and Word) and select and copy the first 500 characters.
    myWord.Selection.HomeKey(constants.wdStory)
    myWord.Selection.MoveEnd(constants.wdCharacter, 500)
    sText = myWord.Selection.Text
However when I try to print sText (or write it to a file
output.write(sText)) I get an exception
    UnicodeError: ASCII encoding error: ordinal not in range(128)
I've tried using str
     sText2 = str(sText)
but still get the same error. I've tried wrapping the code with a try:
except UnicodeError: block but then the string is not converted. I'd
appreciate any pointers as to how I can 'clean-up' this string so that I can
output it as an 8-bit character string.
Tks Kirby





More information about the Python-list mailing list