How to Add Hyperlink in Word (Text & Image) with Python Effortlessly


Hyperlinks play an important role in Word documents, whether you're linking to external resources for additional information or to specific sections within the same file for easier navigation. In this article, you'll learn how to add hyperlinks in Word using Python—covering both text and image links. With automation, you can easily create interactive and well-structured documents, perfect for reports, templates, or technical manuals.
How to Add Text Hyperlinks in Word Using Python without Hassle
Text hyperlinks are commonly used in Word documents—for example, to link proper nouns to external websites, reference explanatory pages, or jump to footnotes within the document. You might also link to a company’s official site to help readers access more information easily. In this chapter, I’ll use free Spire.Doc for Python, a user-friendly Python library, to demonstrate how to insert hyperlinks in Word documents with ease.
Steps to add a text hyperlink in Word documents:
Create an instance of the Document class.
Add a section to the document using the Document.AddSection() method.
Insert a new paragraph in the section through the Section.AddParagraph() method.
Add a text hyperlink to the paragraph with the Paragraph.AppendHyerplink(link: str, text: str, type: HyperlinkType) method.
Save the Word document using the Document.SaveToFile() method.
The code below demonstrates how to insert hyperlinks in Word to a webpage, email address, file, and bookmark:
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()
# Add a web link
paragraph.AppendHyperlink("https://casie.hashnode.dev/", "Blog Home Page", HyperlinkType.WebLink)
paragraph.AppendBreak(BreakType.LineBreak)
paragraph.AppendBreak(BreakType.LineBreak)
# Add an email link
paragraph.AppendHyperlink("mailto:casie@hashnode.com", "Mail Me", HyperlinkType.EMailLink)
paragraph.AppendBreak(BreakType.LineBreak)
paragraph.AppendBreak(BreakType.LineBreak)
# Add a file link
filePath = "E:/Administrator/input/presentation.pptx"
paragraph.AppendHyperlink(filePath, "Click to open the presentation", HyperlinkType.FileLink)
paragraph.AppendBreak(BreakType.LineBreak)
paragraph.AppendBreak(BreakType.LineBreak)
# Add another section and create a bookmark
section2 = doc.AddSection()
bookmarkParagraph = section2.AddParagraph()
bookmarkParagraph.AppendText("Here is a bookmark")
start = bookmarkParagraph.AppendBookmarkStart("myBookmark")
bookmarkParagraph.Items.Insert(0, start)
bookmarkParagraph.AppendBookmarkEnd("myBookmark")
# Add a link to the bookmark
paragraph.AppendHyperlink("myBookmark", "Jump to a location inside this document", HyperlinkType.Bookmark)
paragraph.AppendBreak(BreakType.LineBreak)
paragraph.AppendBreak(BreakType.LineBreak)
# Save to file
doc.SaveToFile("/AddTextHyperlink.docx", FileFormat.Docx2019)
doc.Dispose()
How to Add Image Hyperlinks in Word with Python Easily
Image hyperlinks are commonly used in Word documents for more intuitive and engaging navigation—such as linking a profile picture to a personal homepage, or clicking a company logo to jump to its official website. Compared with text links, image links provide a more visual and professional touch. In this chapter, you’ll learn how to add clickable hyperlinks to images in Word using Python with the help of detailed steps.
Steps to insert image hyperlinks in a Word document:
Create an object of the Document class.
Append a new section and a paragraph to the document.
Add an image to the paragraph with the Paragraph.AppendPicture() method.
Add an image hyperlink to the document using the Paragraph.AppendHyerplink(link: str, picture: DocPicture, type: HyperlinkType) method.
Save the Word document.
Here’s how you can add an image in Word that links to a personal homepage:
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()
# Add an image and link it to a web address
image = "F:/avatar.png"
picture = paragraph.AppendPicture(image)
paragraph.AppendHyperlink("https://casie.hashnode.dev/", picture, HyperlinkType.WebLink)
paragraph.AppendBreak(BreakType.LineBreak)
paragraph.AppendBreak(BreakType.LineBreak)
# Save to file
doc.SaveToFile("/AddImageHyperlink.docx", FileFormat.Docx2019)
doc.Dispose()
To Wrap Up
With just a few lines of Python code, you can easily add both text and image hyperlinks to your Word documents. Whether you're linking to a website, a specific location in the file, or enhancing visuals with clickable images, Python automation makes the process efficient and consistent. Try it out to create more interactive and professional documents.
FAQs about Inserting Hyperlinks in Word Documents
Q1: How do you insert a hyperlink into text?
In Word, highlight the text you want to hyperlink. Then right-click and select "Link" or "Hyperlink". In the Insert Hyperlink window, enter the URL, file path, or bookmark in the Address field and click OK.
Q2: What is the shortcut for inserting hyperlinks in Word?
On Windows, press Ctrl + K; on Mac, press Command + K.
Here’s how:
Select the text or image you want to hyperlink.
Press the shortcut key.
In the dialog box, enter the link address and click OK.
Q3: How do I add a hyperlink to a word in Microsoft Forms?
Microsoft Forms doesn’t support embedding hyperlinks into selected text. Instead, you can only paste a full URL (e.g., https://example.com) into the form, and it will automatically become a clickable link.
Subscribe to my newsletter
Read articles from Casie Liu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
