[python-win32] UnicodeEncodingError when print a doc file

cool_go_blue cool_go_blue at yahoo.com
Wed Jun 15 22:21:42 CEST 2011


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...

--- 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, 4:12 PM

cool_go_blue wrote:
> Thank you so much. It works. But where can I find the APIs such as
> Dispatch vs gencache.EnsureDispatch?
>

To a certain extent, this comes from on-the-job experience.  It is
described in the win32com documentation.  There's also a couple of books
on Python in Windows ("Python Programming on Win32" is one).

> Now, I have another problem. Using the auto save, all auto-numbers
> before paragraphs are not read by the following code:
>
> for word in doc.Content.Text.encode("cp1252", "replace").split():
>     print word
>     count += 1
> print "total word: " , count
>
> It works for the manual saved txt file.
>

You aren't using "auto save" here.  You are still reading the document
directly from the application.  The idea was, after you save the
document as text, you go read that text file instead of using
doc.Content.Text.  You aren't THINKING about your problem.

-- 
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/670b5168/attachment-0001.html>


More information about the python-win32 mailing list