吳裕雄--天生自然python學習筆記:python文檔操作自動查找替換 Word 文件中的指定文字


Win32com 組件提供了自動替換 Word 文件中指定文字 的功能 。在使用“查找”
功能替換文字之前,可先清除源文字及目標文字的格式,以免影響替換效果,語法為 :

替換 Word 文件特定文字的語法為 :

 

 

將指定目錄中所有 Word 文件中的所有“方法”都替換為“ 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()

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM