Hyperlinks are an essential component of creating dynamic and interactive Word documents. By linking specific text or objects to other documents, web pages, email addresses, or specific locations within the same document, hyperlinks allow users to navigate through information seamlessly. In this article, you will learn how to add or remove hyperlinks in a Word document in Python using Spire.Doc for Python.
Install Spire.Doc for Python
This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.Doc
If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows
Add Hyperlinks to Word in Python
Spire.Doc for Python offers the Paragraph.AppendHyperlink() method to add a web link, an email link, a file link, or a bookmark link to a piece of text or an image inside a paragraph. The following are the detailed steps.
- Create a Document object.
- Add a section and a paragraph to it.
- Insert a hyperlink based on text using Paragraph.AppendHyerplink(link: str, text: str, type: HyperlinkType) method.
- Add an image to the paragraph using Paragraph.AppendPicture() method.
- Insert a hyperlink based on the image using Paragraph.AppendHyerplink(link: str, picture: DocPicture, type: HyperlinkType) method.
- Save the result document using Document.SaveToFile() method.
- Python
from spire.doc import * from spire.doc.common import * # Create a Word document doc = Document() # Add a section section = doc.AddSection() # Add a paragraph paragraph = section.AddParagraph() paragraph.AppendHyperlink("https://www-iceblue.com/", "Home Page", HyperlinkType.WebLink) # Append line breaks paragraph.AppendBreak(BreakType.LineBreak) paragraph.AppendBreak(BreakType.LineBreak) # Add an email link paragraph.AppendHyperlink("mailto:support@e-iceblue.com", "Mail Us", HyperlinkType.EMailLink) # Append line breaks paragraph.AppendBreak(BreakType.LineBreak) paragraph.AppendBreak(BreakType.LineBreak) # Add a file link filePath = "C:\\Users\\Administrator\\Desktop\\report.xlsx" paragraph.AppendHyperlink(filePath, "Click to open the report", HyperlinkType.FileLink) # Append line breaks paragraph.AppendBreak(BreakType.LineBreak) paragraph.AppendBreak(BreakType.LineBreak) # Add another section and create a bookmark section2 = doc.AddSection() bookmarkParagrapg = section2.AddParagraph() bookmarkParagrapg.AppendText("Here is a bookmark") start = bookmarkParagrapg.AppendBookmarkStart("myBookmark") bookmarkParagrapg.Items.Insert(0, start) bookmarkParagrapg.AppendBookmarkEnd("myBookmark") # Link to the bookmark paragraph.AppendHyperlink("myBookmark", "Jump to a location inside this document", HyperlinkType.Bookmark) # Append line breaks paragraph.AppendBreak(BreakType.LineBreak) paragraph.AppendBreak(BreakType.LineBreak) # Add an image link image = "C:\\Users\\Administrator\\Desktop\\logo.png" picture = paragraph.AppendPicture(image) paragraph.AppendHyperlink("https://www.e-iceblue.com/", picture, HyperlinkType.WebLink) # Save to file doc.SaveToFile("output/CreateHyperlinks.docx", FileFormat.Docx2019); doc.Dispose()
Remove Hyperlinks from Word in Python
To delete all hyperlinks in a Word document at once, you'll need to find all the hyperlinks in the document and then create a custom method FlattenHyperlinks() to flatten them. The following are the detailed steps.
- Create a Document object.
- Load a sample Word document using Document.LoadFromFile() method.
- Find all the hyperlinks in the document using custom method FindAllHyperlinks().
- Loop through the hyperlinks and flatten all of them using custom method FlattenHyperlinks().
- Save the result document using Document.SaveToFile() method.
- Python
from spire.doc import * from spire.doc.common import * # Find all the hyperlinks in a document def FindAllHyperlinks(document): hyperlinks = [] for i in range(document.Sections.Count): section = document.Sections.get_Item(i) for j in range(section.Body.ChildObjects.Count): sec = section.Body.ChildObjects.get_Item(j) if sec.DocumentObjectType == DocumentObjectType.Paragraph: for k in range((sec if isinstance(sec, Paragraph) else None).ChildObjects.Count): para = (sec if isinstance(sec, Paragraph) else None).ChildObjects.get_Item(k) if para.DocumentObjectType == DocumentObjectType.Field: field = para if isinstance(para, Field) else None if field.Type == FieldType.FieldHyperlink: hyperlinks.append(field) return hyperlinks # Flatten the hyperlink fields def FlattenHyperlinks(field): ownerParaIndex = field.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf( field.OwnerParagraph) fieldIndex = field.OwnerParagraph.ChildObjects.IndexOf(field) sepOwnerPara = field.Separator.OwnerParagraph sepOwnerParaIndex = field.Separator.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf( field.Separator.OwnerParagraph) sepIndex = field.Separator.OwnerParagraph.ChildObjects.IndexOf( field.Separator) endIndex = field.End.OwnerParagraph.ChildObjects.IndexOf(field.End) endOwnerParaIndex = field.End.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf( field.End.OwnerParagraph) FormatFieldResultText(field.Separator.OwnerParagraph.OwnerTextBody, sepOwnerParaIndex, endOwnerParaIndex, sepIndex, endIndex) field.End.OwnerParagraph.ChildObjects.RemoveAt(endIndex) for i in range(sepOwnerParaIndex, ownerParaIndex - 1, -1): if i == sepOwnerParaIndex and i == ownerParaIndex: for j in range(sepIndex, fieldIndex - 1, -1): field.OwnerParagraph.ChildObjects.RemoveAt(j) elif i == ownerParaIndex: for j in range(field.OwnerParagraph.ChildObjects.Count - 1, fieldIndex - 1, -1): field.OwnerParagraph.ChildObjects.RemoveAt(j) elif i == sepOwnerParaIndex: for j in range(sepIndex, -1, -1): sepOwnerPara.ChildObjects.RemoveAt(j) else: field.OwnerParagraph.OwnerTextBody.ChildObjects.RemoveAt(i) # Convert fields to text range and clear the text formatting def FormatFieldResultText(ownerBody, sepOwnerParaIndex, endOwnerParaIndex, sepIndex, endIndex): for i in range(sepOwnerParaIndex, endOwnerParaIndex + 1): para = ownerBody.ChildObjects[i] if isinstance( ownerBody.ChildObjects[i], Paragraph) else None if i == sepOwnerParaIndex and i == endOwnerParaIndex: for j in range(sepIndex + 1, endIndex): if isinstance(para.ChildObjects[j], TextRange): FormatText(para.ChildObjects[j]) elif i == sepOwnerParaIndex: for j in range(sepIndex + 1, para.ChildObjects.Count): if isinstance(para.ChildObjects[j], TextRange): FormatText(para.ChildObjects[j]) elif i == endOwnerParaIndex: for j in range(0, endIndex): if isinstance(para.ChildObjects[j], TextRange): FormatText(para.ChildObjects[j]) else: for j, unusedItem in enumerate(para.ChildObjects): if isinstance(para.ChildObjects[j], TextRange): FormatText(para.ChildObjects[j]) # Format text def FormatText(tr): tr.CharacterFormat.TextColor = Color.get_Black() tr.CharacterFormat.UnderlineStyle = UnderlineStyle.none # Create a Document object doc = Document() # Load a Word file doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\test.docx") # Get all hyperlinks hyperlinks = FindAllHyperlinks(doc) # Flatten all hyperlinks for i in range(len(hyperlinks) - 1, -1, -1): FlattenHyperlinks(hyperlinks[i]) # Save to a different file doc.SaveToFile("output/RemoveHyperlinks.docx", FileFormat.Docx) doc.Close()
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.