I have a MS Word document that I am using as a template. After a test is run, certain parts of the Word document need to be replaced with the test results. For example, there is a section in the Word document that looks similar to:
Test Name
Test Case #
Description of Test
Test Procedure #
...
I can modify the template if neccessary, eg, surround the text that needs to be replaced with angle brackets.
Does anyone know what the best way to modify a Word document in Python is? I have installed the
Win32 Extensions for Python, but it is very difficult to figure out how to use. So far the best reference I have found is
Automating Word Using the Word Object Model MSDN reference, which helps a little, but the code is in VB/C# and I don't know where some of the objects come from; eg:
object replaceAll = Word.WdReplace.wdReplaceAll;
Where does the "Word" object come from in the
above code? As you can see, I'm having great difficulty figuring this out. Does anyone know where I can find better/more Python-specific documentation?
I read somewhere using Word Macros may be easier. Is this true? If so, how would I go about writing/running a Macro from Python?
Or is there another, easier way to do this?
Here's what I have so far:
import win32com.client
w = win32com.client.Dispatch("Word.Application")
w.Documents.Open(r"C:\Documents and Settings\jboyle\Desktop\python_scripts\ms.doc")
doc = w.ActiveDocument
w.Selection.Find.ClearFormatting()
w.Selection.Find.Text = 'word'
w.Selection.Find.Replacement.ClearFormatting()
w.Selection.Find.Replacement.Text = 'potato'
#w.Selection.Find.Execute( what goes here??? )
w.Quit()