Remove Hyperlinks from Word using Python

This short article describes how to remove hyperlinks from Word using Python. It covers IDE setup, a step-by-step guide, and sample code to remove all hyperlinks in Word using Python. Information is also provided to enhance the sample code while working with the hyperlinks.

  1. Set up the IDE to use Aspose.Words for Python via .NET for removing hyperlinks
  2. Load the source Word file with hyperlinks into memory using the Document object
  3. Find all the hyperlinks in the loaded Word file, optionally skipping the table of contents links
  4. Store the list of text shown by each hyperlink for later formatting cleanup
  5. Remove all the hyperlinks one by one while keeping their visible text
  6. Remove the hyperlink formatting, such as the blue color and underline, from that text
  7. Check the rest of the text in the document and remove any similar formatting
  8. Save the resultant Word file after removing the hyperlinks and formatting

These steps summarize how to remove links from Word document using Python. Load the source Word file, set a flag to keep or remove the table of contents hyperlinks, collect all hyperlink fields, store each hyperlink’s visible text before unlinking, and finally remove the hyperlink fields while keeping their text in place. Remove the hyperlink formatting from the text shown by each hyperlink, and also clear hyperlink formatting from the remaining runs so that no hyperlink styling is left in the document.

This code demonstrates how to remove all hyperlinks in Word at once using Python. You can enhance it to also remove links attached to images and shapes, remove unused styles from the document when hyperlinks are removed, and differentiate between external hyperlinks and internal bookmark links while removing the hyperlinks. Do not rely only on a bookmark name starting with _Toc to identify table of contents links; also check if the hyperlink is found inside the table of contents field.

This article shows the process to remove hyperlinks. To accept all the changes tracked in a Word file, refer to the article Accept all changes in Word using Python.