Manage Hyperlinks in Presentations with Python
Overview
A hyperlink is a reference to an external resource, an object or data item, or a specific location within a file. Common hyperlink types in PowerPoint presentations include:
- Links to websites embedded in text, shapes, or media
- Links to slides
Aspose.Slides for Python via .NET enables a wide range of hyperlink-related operations in presentations.
Add URL Hyperlinks
This section explains how to add URL hyperlinks to slide elements when working with Aspose.Slides. It covers assigning link addresses to text, shapes, and pictures to ensure smooth navigation during presentations.
Add URL Hyperlinks to Text
The following code example shows how to add a website hyperlink to text:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 100, 100, 600, 50, False)
shape.add_text_frame("Aspose: File Format APIs")
text_portion = shape.text_frame.paragraphs[0].portions[0]
text_portion.portion_format.hyperlink_click = slides.Hyperlink("https://www.aspose.com/")
text_portion.portion_format.hyperlink_click.tooltip = "More than 70% of Fortune 100 companies trust Aspose APIs."
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
Add URL Hyperlinks to Shapes or Frames
The following code example shows how to add a website hyperlink to a shape:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 100, 100, 600, 50)
shape.hyperlink_click = slides.Hyperlink("https://www.aspose.com/")
shape.hyperlink_click.tooltip = "More than 70% of Fortune 100 companies trust Aspose APIs."
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
Add URL Hyperlinks to Media
Aspose.Slides lets you add hyperlinks to images, audio, and video files.
The following code example shows how to add a hyperlink to an image:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
# Add an image to the presentation.
with open("image.jpeg", "rb") as image_stream:
image_data = image_stream.read()
image = presentation.images.add_image(image_data)
# Create a picture frame on slide 1 using the image added earlier.
picture_frame = slide.shapes.add_picture_frame(slides.ShapeType.RECTANGLE, 10, 10, 100, 100, image)
picture_frame.hyperlink_click = slides.Hyperlink("https://www.aspose.com/")
picture_frame.hyperlink_click.tooltip = "More than 70% of Fortune 100 companies trust Aspose APIs."
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
The following code example shows how to add a hyperlink to an audio file:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
with open("audio.mp3", "rb") as audio_stream:
audio_data = audio_stream.read()
audio = presentation.audios.add_audio(audio_data)
audio_frame = slide.shapes.add_audio_frame_embedded(10, 10, 100, 100, audio)
audio_frame.hyperlink_click = slides.Hyperlink("https://www.aspose.com/")
audio_frame.hyperlink_click.tooltip = "More than 70% of Fortune 100 companies trust Aspose APIs."
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
The following code example shows how to add a hyperlink to a video:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
with open("video.avi", "rb") as video_stream:
video_data = video_stream.read()
video = presentation.videos.add_video(video_data)
video_frame = slide.shapes.add_video_frame(10, 10, 100, 100, video)
video_frame.hyperlink_click = slides.Hyperlink("https://www.aspose.com/")
video_frame.hyperlink_click.tooltip = "More than 70% of Fortune 100 companies trust Aspose APIs."
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
Tip
You may want to see Manage OLE in Presentations Using Python.Use Hyperlinks to Create a Table of Contents
Because hyperlinks let you reference objects or locations, you can use them to build a table of contents.
The sample code below shows how to create a table of contents with hyperlinks:
import aspose.slides as slides
with slides.Presentation() as presentation:
first_slide = presentation.slides[0]
second_slide = presentation.slides.add_empty_slide(first_slide.layout_slide)
content_table = first_slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 40, 40, 300, 100)
content_table.fill_format.fill_type = slides.FillType.NO_FILL
content_table.line_format.fill_format.fill_type = slides.FillType.NO_FILL
content_table.text_frame.paragraphs.clear()
paragraph = slides.Paragraph()
paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.black
paragraph.text = "Title of slide 2 .......... "
link_text_portion = slides.Portion()
link_text_portion.text = "Page 2"
link_text_portion.portion_format.hyperlink_manager.set_internal_hyperlink_click(second_slide)
paragraph.portions.add(link_text_portion)
content_table.text_frame.paragraphs.add(paragraph)
presentation.save("link_to_slide.pptx", slides.export.SaveFormat.PPTX)
Format Hyperlinks
This section shows how to format the appearance of hyperlinks in Aspose.Slides. You’ll learn to control color and other style options to keep hyperlink formatting consistent across text, shapes, and pictures.
Hyperlink Color
Using the color_source property of the Hyperlink class, you can set a hyperlink’s color and read its color information. This feature was introduced in PowerPoint 2019, so changes made through this property do not apply to earlier versions of PowerPoint.
The following sample demonstrates how to add hyperlinks with different colors to the same slide:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape1 = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 100, 100, 600, 50, False)
shape1.add_text_frame("This is a sample of a colored hyperlink.")
text_portion1 = shape1.text_frame.paragraphs[0].portions[0]
text_portion1.portion_format.hyperlink_click = slides.Hyperlink("https://www.aspose.com/")
text_portion1.portion_format.hyperlink_click.color_source = slides.HyperlinkColorSource.PORTION_FORMAT
text_portion1.portion_format.fill_format.fill_type = slides.FillType.SOLID
text_portion1.portion_format.fill_format.solid_fill_color.color = draw.Color.red
shape2 = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 100, 200, 450, 50, False)
shape2.add_text_frame("This is a sample of a regular hyperlink.")
text_portion2 = shape2.text_frame.paragraphs[0].portions[0]
text_portion2.portion_format.hyperlink_click = slides.Hyperlink("https://www.aspose.com/")
presentation.save("hyperlinks.pptx", slides.export.SaveFormat.PPTX)
Removе Hyperlinks from Presentations
This section explains how to remove hyperlinks from presentations when working with Aspose.Slides. You’ll learn how to clear link targets from text, shapes, and pictures while preserving the original content and formatting.
Removе Hyperlinks from Text
The following sample code shows how to remove hyperlinks from text on a presentation slide:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
for shape in slide.shapes:
if type(shape) is slides.AutoShape:
for paragraph in shape.text_frame.paragraphs:
for text_portion in paragraph.portions:
text_portion.portion_format.hyperlink_manager.remove_hyperlink_click()
presentation.save("removed_hyperlinks.pptx", slides.export.SaveFormat.PPTX)
Remove Hyperlinks from Shapes or Frames
The following sample code shows how to remove hyperlinks from shapes on a presentation slide:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
for shape in slide.shapes:
shape.hyperlink_manager.remove_hyperlink_click()
presentation.save("removed_hyperlinks.pptx", slides.export.SaveFormat.PPTX)
Mutable Hyperlinks
The Hyperlink class is mutable. Using this class, you can change the values of these properties:
The following code snippet shows how to add a hyperlink to a slide and then edit its tooltip:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 100, 100, 600, 50, False)
shape.add_text_frame("Aspose: File Format APIs")
text_portion = shape.text_frame.paragraphs[0].portions[0]
text_portion.portion_format.hyperlink_click = slides.Hyperlink("https://www.aspose.com/")
text_portion.portion_format.hyperlink_click.tooltip = "More than 70% of Fortune 100 companies trust Aspose APIs."
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
Supported Properties in IHyperlinkQueries
You can access HyperlinkQueries from the presentation, slide, or text that contains the hyperlink.
The HyperlinkQueries class supports these methods:
FAQ
How can I create internal navigation not just to a slide, but to a “section” or the first slide of a section?
Sections in PowerPoint are groupings of slides; navigation technically targets a specific slide. To “navigate to a section”, you typically link to its first slide.
Can I attach a hyperlink to master slide elements so it works on all slides?
Yes. Master slide and layout elements support hyperlinks. Such links appear on child slides and are clickable during the slideshow.
Will hyperlinks be preserved when exporting to PDF, HTML, images, or video?
In PDF and HTML, yes—links are generally preserved. When exporting to images and video, clickability will not carry over due to the nature of those formats (raster frames/video do not support hyperlinks).