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

Reply via email to