DEV Community

Allen Yang
Allen Yang

Posted on

How to Add, Edit, and Remove Hyperlinks in Excel Using Python

How to Add, Edit, and Remove Hyperlinks in Excel Using Python

When creating data reports, project tracking sheets, or resource indexes, you often need to embed clickable links in Excel cells — such as links to reference websites, email addresses, external files, or other worksheets within the same workbook. Manually adding hyperlinks one by one is not only tedious but also difficult to keep consistent when dealing with large datasets. Automating this process with Python scripts enables you to quickly create, read, modify, and delete hyperlinks in bulk, significantly boosting productivity.

This article demonstrates how to perform various hyperlink operations in Excel worksheets using Spire.XLS for Python, including adding web links, email links, image links, cross-sheet links, and external file links, as well as reading, modifying, and removing existing hyperlinks.

Prerequisites

Before getting started, install Spire.XLS for Python via pip:

pip install Spire.Xls
Enter fullscreen mode Exit fullscreen mode

Then import the required modules in your script:

from spire.xls import *
from spire.xls.common import *
Enter fullscreen mode Exit fullscreen mode

Adding Web and Email Hyperlinks

The most common use of Excel hyperlinks is to associate a URL or an email address with a cell. You can add a hyperlink to a specified cell using the sheet.HyperLinks.Add() method, set the link type via the Type property, and specify the target address through the Address property.

from spire.xls import *
from spire.xls.common import *

workbook = Workbook()
workbook.LoadFromFile("Template.xlsx")
sheet = workbook.Worksheets[0]

# Add a web hyperlink
url_link = sheet.HyperLinks.Add(sheet.Range["D10"])
url_link.TextToDisplay = sheet.Range["D10"].Text
url_link.Type = HyperLinkType.Url
url_link.Address = "http://en.wikipedia.org/wiki/Chicago"

# Add an email hyperlink
mail_link = sheet.HyperLinks.Add(sheet.Range["E10"])
mail_link.TextToDisplay = sheet.Range["E10"].Text
mail_link.Type = HyperLinkType.Url
mail_link.Address = "mailto:contact@example.com"

workbook.SaveToFile("AddHyperlinks.xlsx", ExcelVersion.Version2010)
workbook.Dispose()
Enter fullscreen mode Exit fullscreen mode

The key point here is the TextToDisplay property — it determines the text actually displayed in the cell. Typically, you set it to the cell's existing text so that the hyperlink overlays the cell without altering the original content. Although email links use the HyperLinkType.Url type, the address must be prefixed with the mailto: protocol so that clicking the link automatically opens the default email client.

Adding Image Hyperlinks

In addition to linking text cells, you can also bind hyperlinks to images in a worksheet, enabling effects such as clicking a logo to navigate to a website. Image hyperlinks are set using the SetHyperLink method, where the second parameter set to True indicates an absolute link.

from spire.xls import *
from spire.xls.common import *

workbook = Workbook()
sheet = workbook.Worksheets[0]

# Insert an image and add a hyperlink
picture = sheet.Pictures.Add(2, 1, "logo.png")
picture.SetHyperLink("https://www.example.com", True)

workbook.SaveToFile("ImageHyperlink.xlsx", ExcelVersion.Version2010)
workbook.Dispose()
Enter fullscreen mode Exit fullscreen mode

The first two parameters of Pictures.Add(row, column, imagePath) specify the cell position where the top-left corner of the image is anchored, and the third parameter is the image file path. The second parameter of SetHyperLink, True, indicates that the URL is absolute; if the link points to a relative path resource, you can pass False instead.

Creating Cross-Sheet Links

In Excel files with multiple worksheets, you often need hyperlinks to navigate quickly between different sheets. This can be achieved by setting the link type to HyperLinkType.Workbook and specifying the address in the SheetName!CellAddress format.

from spire.xls import *
from spire.xls.common import *

workbook = Workbook()
sheet = workbook.Worksheets[0]
range = sheet.Range["A1"]

hyperlink = sheet.HyperLinks.Add(range)
hyperlink.Type = HyperLinkType.Workbook
hyperlink.TextToDisplay = "Go to cell C5 in Sheet2"
hyperlink.Address = "Sheet2!C5"

workbook.SaveToFile("CrossSheetLink.xlsx", ExcelVersion.Version2010)
workbook.Dispose()
Enter fullscreen mode Exit fullscreen mode

Note that the Address format is Sheet2!C5 — the sheet name comes before the exclamation mark, and the target cell reference comes after it. This is consistent with the address format used when manually creating cross-sheet hyperlinks in Excel.

Creating External File Links

When you need to reference other files on the local disk (such as reference documents or data source files), set the link type to HyperLinkType.File and point the address to the file path.

from spire.xls import *
from spire.xls.common import *

workbook = Workbook()
sheet = workbook.Worksheets[0]
range = sheet.Range["A1"]

hyperlink = sheet.HyperLinks.Add(range)
hyperlink.Type = HyperLinkType.File
hyperlink.TextToDisplay = "Open External Reference File"
hyperlink.Address = "C:/Documents/reference_data.xlsx"

workbook.SaveToFile("ExternalFileLink.xlsx", ExcelVersion.Version2010)
workbook.Dispose()
Enter fullscreen mode Exit fullscreen mode

When clicked, an external file link opens the target file using the default application associated with its file type in the operating system, making it ideal for linking to PDFs, Excel workbooks, Word documents, and other file types.

Adding Multiple Hyperlinks in Batch

In real-world scenarios, you often need to add multiple hyperlinks to a single worksheet. The following example demonstrates how to add multiple URL and email links in a loop:

from spire.xls import *
from spire.xls.common import *

workbook = Workbook()
workbook.LoadFromFile("Template.xlsx")
sheet = workbook.Worksheets[0]

# Add a homepage link
sheet.Range["B9"].Text = "Homepage"
link1 = sheet.HyperLinks.Add(sheet.Range["B10"])
link1.Type = HyperLinkType.Url
link1.Address = "http://www.example.com"

# Add an email link
sheet.Range["B11"].Text = "Contact Support"
link2 = sheet.HyperLinks.Add(sheet.Range["B12"])
link2.Type = HyperLinkType.Url
link2.Address = "mailto:support@example.com"

# Add a forum link
sheet.Range["B13"].Text = "Forum"
link3 = sheet.HyperLinks.Add(sheet.Range["B14"])
link3.Type = HyperLinkType.Url
link3.Address = "https://www.example.com/forum/"

workbook.SaveToFile("BatchHyperlinks.xlsx", ExcelVersion.Version2010)
workbook.Dispose()
Enter fullscreen mode Exit fullscreen mode

Each hyperlink is bound to its own independent cell range and does not interfere with others. In practice, you can read link data from a database or configuration file and write them in a loop for fully automated hyperlink management.

Reading and Iterating Over Hyperlinks

To retrieve the address and type information of existing hyperlinks in a worksheet, iterate through the sheet.HyperLinks collection to read each link.

from spire.xls import *
from spire.xls.common import *

workbook = Workbook()
workbook.LoadFromFile("HyperlinksSample.xlsx")
sheet = workbook.Worksheets[0]

for link in sheet.HyperLinks:
    address = link.Address
    link_type = link.Type
    print(f"Address: {address}, Type: {link_type}")

workbook.Dispose()
Enter fullscreen mode Exit fullscreen mode

The Type property returns an enumeration value (such as HyperLinkType.Url, HyperLinkType.File, or HyperLinkType.Workbook), which can be used to differentiate between link types and apply the appropriate processing logic. This is particularly useful in data auditing and link validation scenarios — for example, batch-checking the validity of all URL links.

Modifying Hyperlinks

To modify an existing hyperlink, simply retrieve the link object from the HyperLinks collection by index and update the TextToDisplay and Address properties directly.

from spire.xls import *
from spire.xls.common import *

workbook = Workbook()
workbook.LoadFromFile("HyperlinksSample.xlsx")
sheet = workbook.Worksheets[0]

links = sheet.HyperLinks
links[0].TextToDisplay = "Updated Display Text"
links[0].Address = "http://www.new-address.com"

workbook.SaveToFile("ModifiedHyperlinks.xlsx", ExcelVersion.Version2010)
workbook.Dispose()
Enter fullscreen mode Exit fullscreen mode

The HyperLinks collection is indexed in the order hyperlinks were added. To locate a hyperlink for a specific cell, iterate through the collection and compare the Address or the associated cell range to find the target link.

Deleting Hyperlinks

There are two scenarios for deleting hyperlinks: removing only the hyperlink while keeping the cell's display text, or clearing the cell content entirely along with the hyperlink.

from spire.xls import *
from spire.xls.common import *

workbook = Workbook()
workbook.LoadFromFile("HyperlinksSample.xlsx")
sheet = workbook.Worksheets[0]

# Remove the hyperlink only, keeping the text
sheet.HyperLinks.RemoveAt(0)

# Clear all cell content (including text and hyperlinks)
sheet.Range["B1"].ClearAll()

workbook.SaveToFile("RemoveHyperlinks.xlsx", ExcelVersion.Version2010)
workbook.Dispose()
Enter fullscreen mode Exit fullscreen mode

RemoveAt(index) deletes only the hyperlink object itself, leaving the cell text intact. To clear the cell when removing a hyperlink, call the ClearAll() method additionally. Note that when calling RemoveAt repeatedly, the collection index shifts after each deletion. Therefore, when deleting multiple hyperlinks, you should work from the end of the collection toward the beginning, or re-fetch the index after each removal.

Best Practices

  • Choose the correct link type: Use HyperLinkType.Url for web and email links, HyperLinkType.Workbook for cross-sheet links, and HyperLinkType.File for external file links. Selecting the wrong type will cause the link to malfunction.
  • Watch for index shifts during batch deletion: When calling RemoveAt in a loop to delete multiple hyperlinks, iterate from the end of the collection toward the beginning to avoid index misalignment.
  • Email protocol prefix: Email addresses must start with mailto:; otherwise, Excel will not recognize them as email links.
  • Release resources promptly: Call Dispose() after finishing work with a workbook to release file handles and prevent resource leaks when processing multiple files in batch.

Summary

This article covered the full range of hyperlink operations in Excel worksheets using Python: adding web links, email links, image links, cross-sheet links, and external file links, as well as batch insertion, reading, modification, and deletion. The core entry point for all operations is the sheet.HyperLinks collection — Add() for creating, RemoveAt() for deleting, and indexing or iteration for reading and modifying existing hyperlink properties.

Building on these capabilities, you can combine hyperlink operations with other Excel features — for example, batch-adding reference links to auto-generated reports, creating cross-sheet navigation menus for dashboards, or integrating data validation and conditional formatting to build smarter, interactive spreadsheets.

Top comments (0)