See if this gives you the start of word automation using python.
Once you open the document, you can do the following.
After the following code, you can close the document and open another.
Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "test" .Replacement.Text = "test2" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchKashida = False .MatchDiacritics = False .MatchAlefHamza = False .MatchControl = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll
The above code replaces the text "test" with "test2" and performs "replace all".
You can enable other true / false options depending on what you need.
An easy way to find out is to create a macro with the actions you want to take, see the generated code and use it in your own example (with / without changed parameters).
EDIT: by looking at some Matthew code, you can do the following
MSWord.Documents.Open(filename) Selection = MSWord.Selection
And then translate the above VB code to Python.
Note. The following VB code is a shorthand way of assigning a property without using long syntax.
(Vb)
With Selection.Find .Text = "test" .Replacement.Text = "test2" End With
Python
find = Selection.Find find.Text = "test" find.Replacement.Text = "test2"
Forgive my knowledge of python. But I hope you have an idea to move forward. Remember to save and close the document after completing the search / replace operation.
In the end, you can call MSWord.Quit (to free the Word object from memory).
shahkalpesh
source share