<html>
<head>
<style>
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
FONT-SIZE: 10pt;
FONT-FAMILY:Tahoma
}
</style>
</head>
<body class='hmmessage'><div style="text-align: left;">Thanks for the suggestion.<br><br>When I ran the slightly modified code:<br>import win32com.client<br>app = win32com.client.Dispatch('Word.Application')<br>docOrig = app.Documents.Open('n:\\MetroPlanners\\2006\\MCHResearchProject.doc')<br>range = docOrig.Content()<br>range.Find.Text = "e"<br>range.Find.Replacement.Text = "*"<br>range.Find.Execute()<br>docEditted = doc<br>docEditted.SaveAs('n:\\MetroPlanners\\2006\\MCHResearchProjectV2.doc')<br>app.Quit()<br><br><br>I got:<br><br>Traceback (most recent call last):<br>&nbsp; File "N:/SPSS/Python/Examples/MSWord/WordFindAndReplaceV2", line 5, in &lt;module&gt;<br>&nbsp;&nbsp;&nbsp; range.Find.Text = "e"<br>AttributeError: 'unicode' object has no attribute 'Find'<br><br>For what it's worth, I'm running Python 2.5.2<br></div><br>Urban<br><br><hr id="EC_stopSpelling">Date: Tue, 14 Oct 2008 11:32:03 -0400<br>From: bgailer@gmail.com<br>To: bcl@brianlane.com<br>CC: tutor@python.org<br>Subject: Re: [Tutor] Edit a Word document programmatically CORRECTION Open not open<br><br>


<meta http-equiv="Content-Type" content="text/html; charset=unicode">
<meta name="Generator" content="Microsoft SafeHTML">
  


bob gailer wrote:
<blockquote cite="mid:48F4BB00.3030708@gmail.com">
  
Brian C. Lane wrote:
  <blockquote cite="mid:48F4AB40.2050805@brianlane.com">
    <pre>-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

URBAN LANDREMAN wrote:
  </pre>
    <blockquote>
      <pre>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?

    </pre>
    </blockquote>
    <pre>Take a look at the win32com module. Here's a chapter on it from O'Reilly

<a class="EC_moz-txt-link-freetext" href="http://oreilly.com/catalog/pythonwin32/chapter/ch12.html#49339" target="_blank">http://oreilly.com/catalog/pythonwin32/chapter/ch12.html#49339</a>
  </pre>
  </blockquote>
  <br>
That chapter does not really address your question in a simple and
helpful way. Chapter 10 does. Not available online<br>
  <br>
You need some way to run Word from Python and then tell it what to do.<br>
  <br>
pywin32 gives you the tools to run Word. Download it from
  <a class="EC_moz-txt-link-freetext" href="http://sourceforge.net/project/showfiles.php?group_id=78018" target="_blank">http://sourceforge.net/project/showfiles.php?group_id=78018</a>.<br>
  <br>
import win32com.client<br>
word = win32com.dispatch('word.application')<br>
  <br>
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.<br>
  <br>
doc = word.Documents.Open("c:\\test.doc") # provide path to your
document instead,<br>
range = doc.Content()<br>
range.Find.Text = "text to replace"<br>
range.Find.Replacement.Text = "replacement text"<br>
range.Find.Execute()<br>
doc.Save()<br>
  <br>
Give that a try. Let me know what happens. Reply to the list not just
me.<br>
  <br>
  <pre class="EC_moz-signature">-- 
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?
  </pre>
  <pre><hr size="4" width="90%">
_______________________________________________
Tutor maillist  -  <a class="EC_moz-txt-link-abbreviated" href="mailto:Tutor@python.org">Tutor@python.org</a>
<a class="EC_moz-txt-link-freetext" href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a>
  </pre>
</blockquote>
<br>
<br>
<pre class="EC_moz-signature">-- 
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?
</pre>


</body>
</html>