Remove Bookmark in Word using Python

This article explains how to remove bookmark in Word using Python. It contains IDE set-up, programming steps, and sample code showing how to delete a bookmark in Word using Python. You will also get API suggestions to enhance the code for removing specific bookmarks.

Steps to Remove Bookmark from Word DOC using Python

  1. Set up the IDE to use Aspose.Words for Python via .NET to delete bookmarks
  2. Load the Word file with bookmarks into the Document object
  3. Create a list of bookmark names from the document
  4. Gather the hyperlinks from the document and store them in a list
  5. Check all the hyperlinks that point to internal bookmarks in the document
  6. Remove the links that point to bookmarks while keeping the link text
  7. Create a separate list of bookmarks in the document
  8. Remove the bookmarks while keeping the bookmark text in place
  9. Save the updated Word file after removing the bookmarks

These steps summarize how to remove all bookmarks in Word using Python. Load the document containing bookmarks, collect the names of all the bookmarks, create a list of hyperlinks, check which hyperlinks are linked to the bookmarks, and remove the hyperlinks linked to the bookmarks while keeping the link text in place. Create a separate list of bookmarks and delete them while keeping the text, and finally save the document.

Code to Delete Bookmark in Word using Python

This code demonstrates how to remove bookmark in Word using Python. If you want to delete specific bookmarks, use the remove() method, whereas for deleting all the bookmarks without any filter, use the clear() method. The hidden bookmarks with names starting with an underscore, such as _Toc and _GoBack, can be skipped by checking each bookmark’s name.

This article taught you how to remove bookmarks. To remove hyperlinks from a Word file, refer to the article Remove Hyperlinks from Word using Python.