Wu Yuxiong - born natural python learning Notes: python automatic document operation to find the specified text replaces the Word file

Win32com component provides Word automatically replaces the file specified in the text function. In using the "Find" 
before the replacement text features, can first remove the source text and target text format, so as not to affect substitution effect, the syntax is:

Replace Word document specific text syntax is:

 

 

The specified directory of all Word files of all "method" are replaced with "method".
import os
from win32com import client as client
from win32com.client import constants

word = client.gencache.EnsureDispatch('Word.Application')
word.Visible = 1
word.DisplayAlerts = 0
# cpath=os.path.dirname(__file__)
doc = word.Documents.Open("F:\\pythonBase\\pythonex\\ch08\\media\\clipgraph.docx")
word.Selection.Find.ClearFormatting()
word.Selection.Find.Replacement.ClearFormatting()
word.Selection.Find.Execute("方法",False,False,False,False,False,True,constants.wdFindContinue,False,"method",constants.wdReplaceAll)
#doc.Close()
#word.Quit()

 

 

Guess you like

Origin www.cnblogs.com/tszr/p/12030318.html