Thanks for the suggestion.
When I ran the slightly modified code:
import win32com.client
app = win32com.client.Dispatch('Word.Application')
docOrig = app.Documents.Open('n:\\MetroPlanners\\2006\\MCHResearchProject.doc')
range = docOrig.Content()
range.Find.Text = "e"
range.Find.Replacement.Text = "*"
range.Find.Execute()
docEditted = doc
docEditted.SaveAs('n:\\MetroPlanners\\2006\\MCHResearchProjectV2.doc')
app.Quit()
I got:
Traceback (most recent call last):
File "N:/SPSS/Python/Examples/MSWord/WordFindAndReplaceV2", line 5, in
<module>
range.Find.Text = "e"
AttributeError: 'unicode' object has no attribute 'Find'
For what it's worth, I'm running Python 2.5.2
Urban
Date: Tue, 14 Oct 2008 11:32:03 -0400
From: [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
CC: [email protected]
Subject: Re: [Tutor] Edit a Word document programmatically CORRECTION Open not
open
bob gailer wrote:
Brian C. Lane wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
URBAN LANDREMAN wrote:
Good morning,
I'm trying to write some Python code to programmatically do a Find and
Replace of a string within a Microsoft Word document.
Any suggestions on where I could look to find how to do such a function?
Take a look at the win32com module. Here's a chapter on it from O'Reilly
http://oreilly.com/catalog/pythonwin32/chapter/ch12.html#49339
That chapter does not really address your question in a simple and
helpful way. Chapter 10 does. Not available online
You need some way to run Word from Python and then tell it what to do.
pywin32 gives you the tools to run Word. Download it from
http://sourceforge.net/project/showfiles.php?group_id=78018.
import win32com.client
word = win32com.dispatch('word.application')
Now you have a COM connection to Word and can invoke the equivalent of
Visual Basic for Applications to manipulate the Word Document Object
Model. O'Reilly Learning Word Programming gives all the gory details.
Unfortunately for me I don't have Word installed at the moment so I
can't give you tested code.
doc = word.Documents.Open("c:\\test.doc") # provide path to your
document instead,
range = doc.Content()
range.Find.Text = "text to replace"
range.Find.Replacement.Text = "replacement text"
range.Find.Execute()
doc.Save()
Give that a try. Let me know what happens. Reply to the list not just
me.
--
Bob Gailer
Chapel Hill NC
919-636-4239
When we take the time to be aware of our feelings and
needs we have more satisfying interatctions with others.
Nonviolent Communication provides tools for this awareness.
As a coach and trainer I can assist you in learning this process.
What is YOUR biggest relationship challenge?
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor
--
Bob Gailer
Chapel Hill NC
919-636-4239
When we take the time to be aware of our feelings and
needs we have more satisfying interatctions with others.
Nonviolent Communication provides tools for this awareness.
As a coach and trainer I can assist you in learning this process.
What is YOUR biggest relationship challenge?
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor