[python-win32] UnicodeEncodingError when print a doc file

Tom Hawkins Tom.Hawkins at innospecinc.com
Thu Jun 16 09:15:07 CEST 2011


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

Yes of course, you can use the ordinary Python file functions as
described here:
http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-fil
es
For example:

	myfile =
open(r'D:\projects\Myself\HelloPython\src\Drugreservoir.txt')
	filetext = myfile.read()
	myfile.close()

will read the entire contents of the text file into the Python variable
filetext.

If you just want to get the word count of this text, you don't need to
use a loop, by the way - you can get it from:

	wordcount = len(filetext.split())

Tom Hawkins
Principal Scientist
Innospec Inc
Tel: +44 (0)151 356 6197
Fax: +44 (0)151 356 6112


More information about the python-win32 mailing list