python - Can I use Win32 COM to replace text inside a word document? -


I have to make a large number of replacements in some documents, and this is it, I want to be able to automatically enable that work Some documents contain normal strings, and if it can be automated it will be very useful. Com has been read so far, com could be a way to do this, but I do not know if text replacement is supported or not. I want to be able to do this work in dragon? Is this possible? Can you post a code snippet showing, how can you access the text of the document?

Thanks

See if you start using word parsing on word automation We do.

Once you open a document, you can do the following. After the following code, you can use the document & amp; Open the other.

  Selection. Search. Clear formatting selection Find out Replacement. Clear formatting with selection.Find Text = "test" .Positionment. Text = "test2" .forward = true.ORP = wdfindContinue .form = false .MatchCase = False .MatchWholeWord = False .MatchDiacritics = False .MatchAlefHamza = False .MatchControl = False .MatchWildcards = False .MatchSoundsLike = False Change all WordForms = False End.Find.Execute with the computer: = WdReplaceAll  

replaces the "test" text with the above code "test2" and "changes all" Does.
You can make other choices correct / wrong which can change depending on what you need.

This is an easy way to learn that you create a macro with the actions you want to take, see the public code & amp;

Edit: After seeing some code by Matthew, you can do the following

  Open MSWord.Documents (filename) = Select MSWorld. Select  

and then translate the above VB code into Python. Note: The following VB code is the shortcut method of specifying properties without using syntax syntax.

(VB)

  with selection. Find out. Text = "test". Replacement. Ending with text = "Exam 2"  

Forgive my dragon's knowledge. But, I hope you get the idea of ​​moving forward.
Save one & amp; Close the document after you are done with the search / replace function.

Finally, you can call MSWord.Quit (word object to be released from memory).


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -