Insert Hyperlinks Dynamically

Overview

GroupDocs.Assembly for Python via .NET enables you to insert hyperlinks dynamically into documents using link tags. Hyperlinks can point to external URLs, bookmarks within the document, or specific cells/slides depending on the document type.

The main classes involved are:

Here are the steps to insert hyperlinks dynamically:

  • Create a template document with link tags
  • Use the syntax <<link [uri_expression][display_text_expression]>>
  • Provide URI and optional display text from your data source
  • Assemble the document
Note
Link tags are replaced with clickable hyperlinks during document assembly. The URI expression is mandatory, while display text is optional.

Insert a hyperlink with both URL and display text:

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

website = {
    "Name": "Visit our website",
    "Url": "https://harborlane.example/",
}

assembler = DocumentAssembler()
assembler.assemble_document("website-link.docx", "website-link-report.docx",
                            DataSourceInfo(website, "website"))

print("Hyperlink inserted successfully.")

The following sample file is used in this example: website-link.docx

Binary file (DOCX, 7 KB)

Download full output

In your template (website-link.docx), use link tags:

Please <<link [website.Url][website.Name]>> for more information.

After assembly:

Please Visit our website for more information.

(The text “Visit our website” will be a clickable hyperlink to https://harborlane.example/.)

If you omit the display text, the URL is used as the display text:

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

website = {"Url": "https://harborlane.example/"}

assembler = DocumentAssembler()
assembler.assemble_document("website-url-link.docx", "website-url-link-report.docx",
                            DataSourceInfo(website, "website"))

print("Hyperlink with URL only inserted successfully.")

The following sample file is used in this example: website-url-link.docx

Binary file (DOCX, 7 KB)

Download full output

Template:

Visit <<link [website.Url]>> for more information.

Insert hyperlinks for each item in a collection:

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

websites = [
    {"Name": "Home", "Url": "https://harborlane.example/"},
    {"Name": "Products", "Url": "https://harborlane.example/products"},
    {"Name": "Contact", "Url": "https://harborlane.example/contact"},
]

assembler = DocumentAssembler()
assembler.assemble_document("website-links.docx", "website-links-report.docx",
                            DataSourceInfo(websites, "websites"))

print("Hyperlinks inserted from collection successfully.")

The following sample file is used in this example: website-links.docx

Binary file (DOCX, 7 KB)

Download full output

Template with foreach:

Links:
<<foreach [in websites]>>- <<link [Url][Name]>>
<</foreach>>

Link to bookmarks within the same document. When the URI expression gives a bookmark name instead of an address, the link jumps to that bookmark:

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

section = {
    "Title": "Go to Introduction",
    "BookmarkName": "Introduction",
}

assembler = DocumentAssembler()
assembler.assemble_document("bookmark-link.docx", "bookmark-link-report.docx",
                            DataSourceInfo(section, "section"))

print("Bookmark hyperlink inserted successfully.")

The following sample file is used in this example: bookmark-link.docx

Binary file (DOCX, 7 KB)

Download full output

Template:

<<link [section.BookmarkName][section.Title]>>

In this template, the bookmark is on the heading of the second page, and the template creates it too, with a bookmark tag: <<bookmark [section.BookmarkName]>>Introduction<</bookmark>>. A bookmark set in Word works the same way.

Link to cells in spreadsheet documents:

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

data = {"CellReference": "A1", "DisplayText": "Go to Home"}

assembler = DocumentAssembler()
assembler.assemble_document("cell-link.xlsx", "cell-link-report.xlsx",
                            DataSourceInfo(data, "data"))

print("Cell hyperlink inserted successfully.")

The following sample file is used in this example: cell-link.xlsx

Binary file (XLSX, 7 KB)

Download full output

Template (cell A3):

<<link [data.CellReference][data.DisplayText]>>

The link gets the text “Go to Home” and points to cell A1 of the same worksheet (Sheet1!A1). The tag can also hold the reference itself: <<link ["A1"]["Go to Home"]>>.

Link to slides in presentation documents:

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

data = {"SlideNumber": "Slide1", "DisplayText": "Go to First Slide"}

assembler = DocumentAssembler()
assembler.assemble_document("slide-link.pptx", "slide-link-report.pptx",
                            DataSourceInfo(data, "data"))

print("Slide hyperlink inserted successfully.")

The following sample file is used in this example: slide-link.pptx

Binary file (PPTX, 31 KB)

Download full output

Template (second slide):

<<link [data.SlideNumber][data.DisplayText]>>

The link on the second slide jumps to the first one. The tag can also hold the target itself: <<link ["Slide1"]["Go to First Slide"]>>.

Using Uri Objects

A .NET program can pass a System.Uri object as the address. A Python dict holds the address as a str, as all the examples above do; the template needs nothing else.

Warning
Link tags cannot be used within charts. For hyperlinks in charts, use alternative approaches or place links outside chart elements.

Advanced Usage Topics

To learn more about bookmark creation, complex hyperlink scenarios, and advanced link formatting, please refer to the advanced usage section.

More resources

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online Apps

Along with the full-featured library, we provide simple but powerful free online apps.

To assemble documents from templates and data sources, you can use the online apps from the GroupDocs.Assembly App Product Family.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.