Insert Hyperlinks into Excel or OpenOffice
How to Add Hyperlinks
Aspose.Cells for Python via .NET allows developers to add hyperlinks to Excel files either using the API or designer spreadsheets(spreadsheets where hyperlinks are created manually and Aspose.Cells for Python via .NET is used to import them into other spreadsheets).
Aspose.Cells for Python via .NET provides a class, Workbook that represents a Microsoft Excel file. The Workbook class contains a WorksheetCollection that allows access to each worksheet in the Excel file. A worksheet is represented by the Worksheet class. The Worksheet class provides different methods for adding different hyperlinks to Excel files.
How to Add a Link to a URL
The Worksheet class contains a hyperlinks collection. Each item in the hyperlinks collection represents a Hyperlink. Add hyperlinks to URLs by calling the hyperlinks collection’s add method. The add method takes the following parameters:
- Cell name, the name of the cell the hyperlink will be added to.
- Number of rows, the number of rows in this hyperlink range.
- Number of columns, the number of columns in this hyperlink range
- URL, the URL address.
from aspose.cells import Workbook | |
from os import os, path | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Obtaining the reference of the first worksheet | |
worksheet = workbook.worksheets[0] | |
# Adding a hyperlink to a URL at "A1" cell | |
worksheet.hyperlinks.add("A1", 1, 1, "http:// Www.aspose.com") | |
# Saving the Excel file | |
workbook.save(dataDir + "output.out.xls") |
How to Add a Link to a Cell in the Same File
It is possible to add hyperlinks to cells in the same Excel file by calling the hyperlinks collection’s add method. The add method works for both internal and external hyperlinks. One version of the overloaded method takes the following parameters:
- Cell name,the name of the cell the hyperlink will be added to.
- Number of rows, the number of rows in this hyperlink range.
- Number of columns, the number of columns in this hyperlink range.
- URL, the address of the target cell.
from aspose.cells import Workbook | |
from os import os, path | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Workbook object | |
workbook.worksheets.add() | |
# Obtaining the reference of the first (default) worksheet | |
worksheet = workbook.worksheets[0] | |
# Adding an internal hyperlink to the "B9" cell of the other worksheet "Sheet2" in | |
# The same Excel file | |
worksheet.hyperlinks.add("B3", 1, 1, "Sheet2!B9") | |
# Saving the Excel file | |
workbook.save(dataDir + "output.out.xls") |
How to Add a Link to an External File
It is possible to add hyperlinks to external Excel files by calling the hyperlinks collection’s add method. The add method takes the following parameters:
- Cell name, the name of the cell the hyperlink will be added to.
- Number of rows, the number of rows in this hyperlink range.
- Number of columns, the number of columns in this hyperlink range.
- URL, the address of the target, external Excel file.
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Excel object | |
i = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[i] | |
# Adding an internal hyperlink to the "B9" cell of the other worksheet "Sheet2" in | |
# The same Excel file | |
worksheet.hyperlinks.add("A5", 1, 1, dataDir + "book1.xls") | |
# Saving the Excel file | |
workbook.save(dataDir + "output.out.xls") |