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.
If you omit the display text, the URL is used as the display text:
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssemblerwebsite={"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.")
Visit <<link [website.Url]>> for more information.
Insert Multiple Hyperlinks from Collection
Insert hyperlinks for each item in a collection:
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssemblerwebsites=[{"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.")
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.
Hyperlink to Cell (Spreadsheet Documents)
Link to cells in spreadsheet documents:
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssemblerdata={"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
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"]>>.
Hyperlink to Slide (Presentation Documents)
Link to slides in presentation documents:
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssemblerdata={"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
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: