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

前端 未结 5 1542
醉话见心
醉话见心 2020-12-28 10:58

I have to perform a large number of replacements in some documents, and the thing is, I would like to be able to automate that task. Some of the documents contain common str

5条回答
  •  星月不相逢
    2020-12-28 11:49

    If this mailing list post is right, accessing the document's text is a 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 for and Replace Text in Documents. The examples use VB and C#, but the basics should apply to Python too.

提交回复
热议问题