Can I use Win32 COM to replace text inside a Word document?

I need to make a large number of replacements in some documents, and the fact is that I would like to be able to automate this task. Some of the documents contain common lines, and this would be very useful if it could be automated. From what I have read so far, COM may be one way to do this, but I don't know if text replacement is supported. I would like to accomplish this task in python? Is it possible? Could you post a code snippet showing how to access the text of the document?

Thanks!

+4
python replace winapi ms-word com
source share
5 answers

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).

+8
source share

I like the answers so far;

here's a proven example (slightly modified from here )
which replaces all occurrences of a string in 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 app.Documents.Open(word_file) # 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) app.ActiveDocument.Close(SaveChanges=True) app.Quit() f = 'c:/path/to/my/word.doc' search_replace_all(f, 'string_to_be_replaced', 'replacement_str') 
+10
source share

If this mailing list is right, access to the text of the document is simple as:

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

Also see How to search and replace text in documents . The examples use VB and C #, but the basics should also apply to Python.

+3
source share

Check out this link: http://python.net/crew/pirx/spam7/

The links on the left side indicate the documentation.

You can generalize this using the object model, which is located here:

http://msdn.microsoft.com/en-us/library/kw65a0we(VS.80).aspx

+2
source share

You can also achieve this using VBScript . Just enter the code into a file called script.vbs , then open a command prompt (Start β†’ Run β†’ Cmd), then go to the folder where the script is located and enter:

 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 objDoc.Close Wscript.Echo "Ready" End If Next objWord.Quit 
+2
source share

All Articles