Add, Update, Extract or Delete Hyperlinks in Excel with Python

Alice Yang
5 min readJun 20, 2024

--

Hyperlinks are more than just simple clickable text or images — they are the connective tissue that can transform a static spreadsheet into a dynamic, interconnected information hub. With hyperlinks, you can seamlessly link different parts of your Excel workbook, allowing users to navigate between related data, analyses, and supporting documentation with ease. Moreover, hyperlinks provide a gateway to the broader digital landscape, enabling you to incorporate external web pages, files, and other resources directly into your spreadsheets. In this article, we will explore how to add, update, extract and delete hyperlinks in Excel using Python.

Python Library to Add, Update, Extract or Delete Hyperlinks in Excel

To add, update, extract, and delete hyperlinks in Excel with Python, we can use the Spire.XLS for Python library.

Spire.XLS for Python is an easy-to-use and feature-rich library for creating, reading, editing, and converting Excel files within Python applications. With this library, you can work with a lot of spreadsheet formats, such as XLS, XLSX, XLSB, XLSM, and ODS. Moreover, you are also able to render Excel files to other types of file formats, such as PDF, HTML, CSV, Text, Image, XML, SVG, ODS, PostScript, and XPS.

You can install Spire.XLS for Python from PyPI by running the following commands in your terminal:

pip install Spire.Xls

For more detailed information about the installation, you can check these official documentations:

Add Hyperlinks to Excel with Python

With Spire.XLS for Python, you can programmatically add text hyperlinks that connect to:

  • External websites
  • Email addresses
  • External Files
  • Specific parts within the Excel file
  • UNC addresses

In addition to text-based hyperlinks, Spire.XLS for Python also enables you to create image hyperlinks, allowing you to transform images in your Excel worksheets into clickable links.

The example below shows how to add text hyperlinks and image hyperlinks to an Excel file with Python:

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

# Create an object of the Workbook class
workbook = Workbook()

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Add a text hyperlink that connects to an external website
cell1 = sheet.Range["B2"]
webLink = sheet.HyperLinks.Add(cell1)
webLink.Type = HyperLinkType.Url
webLink.TextToDisplay = "Medium.com"
webLink.Address = "https://medium.com/"

# Add a text hyperlink that connects to an email address
cell2 = sheet.Range["B4"]
mailLink = sheet.HyperLinks.Add(cell2)
mailLink.Type = HyperLinkType.Url
mailLink.TextToDisplay = "Contact Us"
mailLink.Address = "support@mycompany.com"

# Add a text hyperlink that connects to an external file
cell3 = sheet.Range["B6"]
fileLink = sheet.HyperLinks.Add(cell3)
fileLink.Type = HyperLinkType.File
fileLink.TextToDisplay = "Open Report.xlsx"
fileLink.Address = "C:\\Users\\Administrator\\Desktop\\Report.xlsx"

# Add a text hyperlink that connects to a cell of another sheet in the same workbook
cell4 = sheet.Range["B8"]
linkToSheet = sheet.HyperLinks.Add(cell4)
linkToSheet.Type = HyperLinkType.Workbook
linkToSheet.TextToDisplay = "Go to Sheet2!A1"
linkToSheet.Address = "Sheet2!A1"

# Insert an image into the worksheet
image = sheet.Pictures.Add(10, 2, "Logo.png")
# image.Width = 50
# image.Height = 50
image.LeftColumnOffset = 25
image.TopRowOffset = 25
# Add a hyperlink to the image
image.SetHyperLink("https://medium.com/", True)

# Set the width of the second column
sheet.SetColumnWidth(2, 17)
# Set the height of the tenth row
sheet.SetRowHeight(10, image.Height)

# Save the resulting file
workbook.SaveToFile("AddHyperlinks.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
Add Hyperlinks to Excel with Python

Update Hyperlinks in Excel with Python

Sometimes, you may need to make changes to existing hyperlinks in your Excel worksheet, such as altering the destination URL or updating the display text.

The example below shows how to update the destination URL and the display text of a hyperlink in an Excel file with Python:

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

# Create an object of the Workbook class
workbook = Workbook()
# Load an Excel file containing hyperlinks
workbook.LoadFromFile("AddHyperlinks.xlsx")

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Get the first hyperlink
link = sheet.HyperLinks[0]

# Or get the hyperlink in a specific cell
# link = sheet.Range["B2"].Hyperlinks[0]

# Update the display text of the hyperlink
link.TextToDisplay = "Google.com"
# Update the destination URL of the hyperlink
link.Address = "https://www.google.com/"

# Save the workbook to a file
workbook.SaveToFile("UpdateHyperlink.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

Extract Hyperlinks from Excel with Python

Extracting hyperlinks from Excel can be useful when you need to work with the hyperlink addresses separately or perform specific actions on them.

The example below shows how to extract the hyperlinks from an Excel file with Python:

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

# Create an object of the Workbook class
workbook = Workbook()
# Load an Excel file containing hyperlinks
workbook.LoadFromFile("AddHyperlinks.xlsx")

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Get the hyperlinks in the worksheet
linkCollection = sheet.HyperLinks

# Create a list to store the extracted hyperlink information
hyperlinks = []

# Extract the hyperlink information
for link in linkCollection:
# Get the display text of each hyperlink
displayText = link.TextToDisplay
# Get the address of each hyperlink
address = link.Address
# Append the display text and address to the list
hyperlinks.append("Display Text: " + displayText)
hyperlinks.append("Address: " + address)
hyperlinks.append("")

# Specify the output file path
output_file = "hyperlinks.txt"

# Write the hyperlink information to the text file
with open(output_file, "w", encoding="utf-8") as file:
for hyperlink in hyperlinks:
file.write(hyperlink + "\n")

print(f"Hyperlinks saved to '{output_file}'.")
workbook.Dispose()

Delete Hyperlinks from Excel with Python

Hyperlinks can sometimes clutter the worksheet, making it harder to read and navigate the content. Removing unwanted hyperlinks can help keep the worksheet clean and organized.

The example below shows how to remove the hyperlinks from an Excel file with Python:

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

# Create an object of the Workbook class
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("AddHyperlinks.xlsx")

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Get the hyperlinks in the worksheet
linkCollection = sheet.HyperLinks

# Remove the hyperlinks
for i in range(linkCollection.Count - 1, -1, -1):
sheet.HyperLinks.RemoveAt(i)

# Or remove the hyperlink from a specific cell
# sheet.Range["B2"].Hyperlinks.RemoveAt(0)

# Save the workbook to a file
workbook.SaveToFile("DeleteHyperlinks.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

Conclusion

This article demonstrated how to insert, update, extract or delete hyperlinks in Excel files using Python. We hope you find it helpful.

Related Topics

--

--

Alice Yang

Skilled senior software developers with five years of experience in all phases of software development life cycle using .NET, Java and C++ languages.