[python-win32] UnicodeEncodingError when print a doc file

cool_go_blue cool_go_blue at yahoo.com
Thu Jun 16 03:03:14 CEST 2011


THanks. After replying to your email, I thought about what you wrote, just like what you said in the following email. So, for a normal text file, just use open to read the file. There is no need to use the way to open a word document via win32com to read a text file. Will win32com consume more time and memory to read a text file than the general input stream? Thanks again.

--- On Wed, 6/15/11, Tim Roberts <timr at probo.com> wrote:

From: Tim Roberts <timr at probo.com>
Subject: Re: [python-win32] UnicodeEncodingError when print a doc file
To: "Python-Win32 List" <python-win32 at python.org>
Date: Wednesday, June 15, 2011, 7:52 PM

cool_go_blue wrote:
> So, my following code:
>
> app = win32com.client.gencache.EnsureDispatch('Word.Application')
> app.Documents.Open(r'D:/projects/Myself/HelloPython/src/Drugreservoir.doc')
> app.ActiveDocument.SaveAs(r'D:/projects/Myself/HelloPython/src/Drugreservoir1.txt',FileFormat=win32com.client.constants.wdFormatText)
> doc =
> app.Documents.Open(r'D:/projects/Myself/HelloPython/src/Drugreservoir1.txt')
> count = 0
> for word in doc.Content.Text.encode("cp1252", "replace").split():
>     print word
>
> Does SaveAs save the doc to a txt file? What I was thinking is
> "app.Documents.Open('D:/projects/Myself/HelloPython/src/Drugreservoir1.txt')"
> can open the txt file which I can read. Is there any way I can read
> the saved txt file after SaveAs...
>

Well, of course there is.  At that point, you have a plain, old,
ordinary text file.  Just use the normal Python techniques to read it.

    doc = open(r'D:/projects/Myself/HelloPython/src/Drugreservoir1.txt')
    for word in doc.read().split():
        print word

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

_______________________________________________
python-win32 mailing list
python-win32 at python.org
http://mail.python.org/mailman/listinfo/python-win32
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20110615/fc34b666/attachment.html>


More information about the python-win32 mailing list