[ACCEPTED]-Can I use Win32 COM to replace text inside a word document?-replace

Accepted answer
Score: 13

I like the answers so far;
here's a tested 3 example (slightly modified from here)
that 2 replaces all occurrences of a string in 1 a Word document:

import win32com.client

def search_replace_all(word_file, find_str, replace_str):
    ''' replace all occurrences of `find_str` w/ `replace_str` in `word_file` '''
    wdFindContinue = 1
    wdReplaceAll = 2

    # Dispatch() attempts to do a GetObject() before creating a new one.
    # DispatchEx() just creates a new one. 
    app = win32com.client.DispatchEx("Word.Application")
    app.Visible = 0
    app.DisplayAlerts = 0

    # expression.Execute(FindText, MatchCase, MatchWholeWord,
    #   MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward, 
    #   Wrap, Format, ReplaceWith, Replace)
    app.Selection.Find.Execute(find_str, False, False, False, False, False, \
        True, wdFindContinue, False, replace_str, wdReplaceAll)

f = 'c:/path/to/my/word.doc'
search_replace_all(f, 'string_to_be_replaced', 'replacement_str')
Score: 9

See if this gives you a start on word automation 22 using python.

Once you open a document, you 21 could do the following.
After the following 20 code, you can Close the document & open 19 another.

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 18 "test" with "test2" and 17 does a "replace all".
You can 16 turn other options true/false depending 15 on what you need.

The simple way to learn 14 this is to create a macro with actions you 13 want to take, see the generated code & use 12 it in your own example (with/without modified 11 parameters).

EDIT: After looking at some 10 code by Matthew, you could do the following

Selection = MSWord.Selection

And 9 then translate the above VB code to Python.
Note: The 8 following VB code is shorthand way of assigning 7 property without using the long syntax.


With Selection.Find
    .Text = "test"
    .Replacement.Text = "test2"
End With


find = Selection.Find
find.Text = "test"
find.Replacement.Text = "test2"

Pardon 6 my python knowledge. But, I hope you get 5 the idea to move forward.
Remember to do 4 a Save & Close on Document, after you 3 are done with the find/replace operation.

In 2 the end, you could call MSWord.Quit (to release Word 1 object from memory).

Score: 3

If this mailing list post is right, accessing the document's text 3 is a simple as:

MSWord = win32com.client.Dispatch("Word.Application")
MSWord.Visible = 0 
docText = MSWord.Documents[0].Content

Also see How to: Search for and Replace Text in Documents. The examples 2 use VB and C#, but the basics should apply 1 to Python too.

Score: 2

Checkout this link: http://python.net/crew/pirx/spam7/

The links on the left 3 side point to the documentation.

You can 2 generalize this using the object model, which 1 is found here:


Score: 2

You can also achieve this using VBScript. Just type 4 the code into a file named script.vbs, then open a 3 command prompt (Start -> Run -> Cmd), then 2 switch to the folder where the script is 1 and type:

cscript script.vbs 

strFolder = "C:\Files"

Const wdFormatDocument  = 0

'Select all files in strFolder
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='" & strFolder & "'} Where " _
        & "ResultClass = CIM_DataFile")

'Start MS Word
Set objWord = CreateObject("Word.Application")

Const wdReplaceAll = 2
Const wdOrientLandscape = 1

For Each objFile in colFiles
    If objFile.Extension = "doc" Then
        strFile = strFolder & "\" & objFile.FileName & "." & objFile.Extension
        strNewFile = strFolder & "\" & objFile.FileName & ".doc"
        Wscript.Echo "Processing " & objFile.Name & "..."

        Set objDoc = objWord.Documents.Open(strFile)

        objDoc.PageSetup.Orientation = wdOrientLandscape

        'Replace text - ^p in a string stands for new paragraph; ^m stands for page break
        Set objSelection = objWord.Selection
        objSelection.Find.Text = "String to replace"
        objSelection.Find.Forward = TRUE
        objSelection.Find.Replacement.Text = "New string"

        objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll

        objDoc.SaveAs strNewFile, wdFormatDocument
        Wscript.Echo "Ready"
    End If


More Related questions