reading character in word

Steve Holden sholden at holdenweb.com
Wed Dec 5 08:35:59 EST 2001


"sebastien" <s.thuriez at quantaflow.com> wrote ...
> Hi,
>
> Maybe have you encountered my problem : I do not manage to read
> accentuated characters such as éèà ... from a microsoft word
> application using win32com.client.
>
> Here is the programm that I used :
>
> from win32com.client import constants, Dispatch
>
> word= Dispatch('Word.Application')
> mondoc=word.Documents.Open(r"c:\test.doc")
> nombre_caracteres=mondoc.Characters.Count
> for numero_caractere in range (1,int(nombre_caracteres)+1):
>     caractere=mondoc.Characters.Item(numero_caractere)
>     try:
>         print caractere
>      except:
>         print("cannot read character")
>         pass
>
The problem here is a lack of information: you are catching all errors, then
not printing out enough information to see what the exact error is! I
suspect it will be complaining about characters with ordinal values greater
than 127 -- the print statement notoriously fails to handle these.
>
> Everything is fine as soon as I do not use characters with coma accent
> such as é à è in the document...
>
>
> I also tried on my PC to do :
>
> test_input=raw_input("entrer phrase avec accents : ")
> print str(test_input)
> for char in test_input:
>     print char,ord(char)
>
> after enterring word using the éàè..there is no problem printing the
> correct letters (for exemple été).
>
> There maybe some option in Word that I should set ??
>
> I hope that someone will have some leads ...

You need to do a search on Google for something like "Python print Unicode
string" to get advice on conversion to an appropriate encoding before you
print. If this isn't the problem, then remove the try/except and post the
exact message you see.

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list