[python-win32] UnicodeEncodingError when print a doc file

Tim Roberts timr at probo.com
Wed Jun 15 21:26:31 CEST 2011


cool_go_blue wrote:
> this is the first three lines of my code:
>
> app = win32com.client.Dispatch('Word.Application')
> app.Documents.Open('D:\projects\Myself\HelloPython\src\Drugreservoir.doc')
> app.ActiveDocument.SaveAs('D:\projects\Myself\HelloPython\src\Drugreservoir1.txt',FileFormat=win32com.client.constants.wdFormatText)
>

If you use this instead:
    app = win32com.client.gencache.EnsureDispatch('Word.Application')
then the framework will build a Python wrapper for objects of that type,
and that Python wrapper will create the constants.

You MUST get in the habit of using the proper escape characters.  The
backslash has special meaning in Python strings.  You have three choices:

1. Use a "raw" string:
   
apps.Documents.Open(r'D:\projects\Myself\HelloPython\src\Drugreservoir.doc')
2. Double the backslashes:
   
apps.Documents.Open('D:\\projects\\Myself\\HelloPython\\src\\Drugreservoir.doc')
3. Use forward slashes:
   
apps.Documents.Open('D:/projects/Myself/HelloPython/src/Drugreservoir.doc')

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the python-win32 mailing list