使用 Python 在 Excel 中添加、更新、提取或删除超链接
超链接不仅仅是简单的可点击文本或图像,它们是可以将静态电子表格转换为动态、互连的信息中心的结缔组织。通过超链接,您可以无缝链接 Excel 工作簿的不同部分,使用户可以轻松地在相关数据、分析和支持文档之间导航。此外,超链接提供了通往更广阔的数字景观的门户,使您能够将外部网页、文件和其他资源直接合并到您的电子表格中。
用于在Excel中添加,更新,提取或删除超链接的Python库
要使用 Python 在 Excel 中添加、更新、提取和删除超链接,我们可以使用 Python 库的Spire.XLS。
Spire.XLS for Python 是一个易于使用且功能丰富的库,用于在 Python 应用程序中创建、读取、编辑和转换 Excel 文件。使用此库,您可以处理许多电子表格格式,例如 XLS、XLSX、XLSB、XLSM 和 ODS。此外,您还可以将 Excel 文件渲染为其他类型的文件格式,例如 PDF、HTML、CSV、文本、图像、XML、SVG、ODS、PostScript 和 XPS。
您可以通过在终端中运行以下命令从 PyPI 安装 Spire.XLS for Python:
pip install Spire.Xls
有关安装的更多详细信息,您可以查看以下官方文档:
- 如何在 Windows 上安装 Spire.XLS for Python
- 如何在 Mac 上安装 Spire.XLS for Python
使用 Python 向 Excel 添加超链接
使用 Spire.XLS for Python,您可以通过编程方式添加连接到以下位置的文本超链接:
- 外部网站
- 电子邮件地址
- 外部文件
- Excel 文件中的特定部分
- UNC 地址
除了基于文本的超链接之外,Spire.XLS for Python 还允许您创建图像超链接,从而允许您将 Excel 工作表中的图像转换为可单击的链接。
下面的示例显示了如何使用 Python 向 Excel 文件添加文本超链接和图像超链接:
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()
使用 Python 向 Excel 添加超链接
使用 Python 更新 Excel 中的超链接
有时,您可能需要更改 Excel 工作表中的现有超链接,例如更改目标 URL 或更新显示文本。
下面的示例显示了如何使用 Python 更新 Excel 文件中超链接的目标 URL 和显示文本:
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()
使用 Python 从 Excel 中提取超链接
当您需要单独使用超链接地址或对其执行特定操作时,从 Excel 中提取超链接可能很有用。
下面的示例显示了如何使用 Python 从 Excel 文件中提取超链接:
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()
使用 Python 从 Excel 中删除超链接
超链接有时会使工作表变得混乱,从而使阅读和导航内容变得更加困难。删除不需要的超链接有助于保持工作表的整洁和有序。
下面的示例显示了如何使用 Python 从 Excel 文件中删除超链接:
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()