简体   繁体   中英

How to insert bookmark in Word using Python (win32com)?

我正在使用win32com,如何在Word文档中插入带有现有文本的书签?

Here you go:

import win32com.client
wordApp = win32com.client.Dispatch('Word.Application') # alternately DispatchEx
wordApp.visible = True                                 # alternately False or leave out
wordDoc = wordApp.Documents.Open('c:/my_word_doc.docx')
myRange = wordDoc.Content
myRange.Find.Execute(FindText='peanuts')
myRange.Bookmarks.Add(Name='X')
wordDoc.SaveAs('c:/my_word_doc.docx')
wordDoc.Close()
wordApp.Quit()

If you don't want Word to be visible during this process use win32com.client.DispatchEx and don't set wordApp.visible to True .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM