Manage Presentation Hyperlinks in Python via Java
Introduction
A hyperlink is a reference to an object, data, or a location. Common hyperlinks in PowerPoint presentations include:
- Links to websites in text, shapes, or media
- Links to slides
Aspose.Slides for Python via Java allows you to perform many tasks involving hyperlinks in presentations.
Note
You may want to check out Aspose’s simple, free online PowerPoint editor.Add URL Hyperlinks
Add URL Hyperlinks to Text
This Python code shows you how to add a website hyperlink to text:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Hyperlink, Presentation, SaveFormat, ShapeType
presentation = Presentation()
try:
shape = presentation.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 600, 50, False)
shape.addTextFrame("Aspose: File Format APIs")
portion_format = shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat()
portion_format.setHyperlinkClick(Hyperlink("https://www.aspose.com/"))
portion_format.getHyperlinkClick().setTooltip("More than 70% Fortune 100 companies trust Aspose APIs")
portion_format.setFontHeight(32)
presentation.save("presentation-out.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Add URL Hyperlinks to Shapes or Frames
This sample code in Python via Java shows you how to add a website hyperlink to a shape:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Hyperlink, Presentation, SaveFormat, ShapeType
presentation = Presentation()
try:
shape = presentation.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 600, 50)
shape.setHyperlinkClick(Hyperlink("https://www.aspose.com/"))
shape.getHyperlinkClick().setTooltip("More than 70% Fortune 100 companies trust Aspose APIs")
presentation.save("pres-out.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Add URL Hyperlinks to Media
Aspose.Slides allows you to add hyperlinks to images, audio, and video files.
This sample code shows you how to add a hyperlink to an image:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Hyperlink, Images, Presentation, SaveFormat, ShapeType
presentation = Presentation()
try:
# Adds image to presentation
image = Images.fromFile("image.png")
try:
picture = presentation.getImages().addImage(image)
finally:
image.dispose()
# Creates picture frame on slide 1 based on previously added image
picture_frame = presentation.getSlides().get_Item(0).getShapes().addPictureFrame(ShapeType.Rectangle, 10, 10, 100, 100, picture)
picture_frame.setHyperlinkClick(Hyperlink("https://www.aspose.com/"))
picture_frame.getHyperlinkClick().setTooltip("More than 70% Fortune 100 companies trust Aspose APIs")
presentation.save("pres-out.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
This sample code shows you how to add a hyperlink to an audio file:
from pathlib import Path
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Hyperlink, Presentation, SaveFormat
presentation = Presentation()
try:
audio_data = Path("audio.mp3").read_bytes()
java_audio_data = jpype.JArray(jpype.JByte)(audio_data)
audio = presentation.getAudios().addAudio(java_audio_data)
audio_frame = presentation.getSlides().get_Item(0).getShapes().addAudioFrameEmbedded(10, 10, 100, 100, audio)
audio_frame.setHyperlinkClick(Hyperlink("https://www.aspose.com/"))
audio_frame.getHyperlinkClick().setTooltip("More than 70% Fortune 100 companies trust Aspose APIs")
presentation.save("pres-out.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
This sample code shows you how to add a hyperlink to a video:
from pathlib import Path
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Hyperlink, Presentation, SaveFormat
presentation = Presentation()
try:
video_data = Path("video.avi").read_bytes()
java_video_data = jpype.JArray(jpype.JByte)(video_data)
video = presentation.getVideos().addVideo(java_video_data)
video_frame = presentation.getSlides().get_Item(0).getShapes().addVideoFrame(10, 10, 100, 100, video)
video_frame.setHyperlinkClick(Hyperlink("https://www.aspose.com/"))
video_frame.getHyperlinkClick().setTooltip("More than 70% Fortune 100 companies trust Aspose APIs")
presentation.save("pres-out.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Tip
You may want to see Manage OLE.Use Hyperlinks to Create a Table of Contents
Since hyperlinks allow you to add references to objects or places, you can use them to create a table of contents.
This sample code shows you how to create a table of contents with hyperlinks:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FillType, Paragraph, Portion, Presentation, SaveFormat, ShapeType
Color = jpype.JClass("java.awt.Color")
presentation = Presentation()
try:
first_slide = presentation.getSlides().get_Item(0)
second_slide = presentation.getSlides().addEmptySlide(first_slide.getLayoutSlide())
content_table = first_slide.getShapes().addAutoShape(ShapeType.Rectangle, 40, 40, 300, 100)
content_table.getFillFormat().setFillType(FillType.NoFill)
content_table.getLineFormat().getFillFormat().setFillType(FillType.NoFill)
content_table.getTextFrame().getParagraphs().clear()
paragraph = Paragraph()
paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
paragraph.setText("Title of slide 2 .......... ")
link_portion = Portion()
link_portion.setText("Page 2")
link_portion.getPortionFormat().getHyperlinkManager().setInternalHyperlinkClick(second_slide)
paragraph.getPortions().add(link_portion)
content_table.getTextFrame().getParagraphs().add(paragraph)
presentation.save("link_to_slide.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Format Hyperlinks
Color
With the Hyperlink.setColorSource property in the Hyperlink class, you can set the color for hyperlinks and also get the color information from hyperlinks. The feature was first introduced in PowerPoint 2019, so changes involving the property do not apply to older PowerPoint versions.
This sample code demonstrates an operation where hyperlinks with different colors are added to the same slide:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FillType, Hyperlink, HyperlinkColorSource, Presentation, SaveFormat, ShapeType
Color = jpype.JClass("java.awt.Color")
presentation = Presentation()
try:
colored_link_shape = presentation.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 450, 50, False)
colored_link_shape.addTextFrame("This is a sample of colored hyperlink.")
portion_format = colored_link_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat()
portion_format.setHyperlinkClick(Hyperlink("https://www.aspose.com/"))
portion_format.getHyperlinkClick().setColorSource(HyperlinkColorSource.PortionFormat)
portion_format.getFillFormat().setFillType(FillType.Solid)
portion_format.getFillFormat().getSolidFillColor().setColor(Color.RED)
default_link_shape = presentation.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 100, 200, 450, 50, False)
default_link_shape.addTextFrame("This is a sample of usual hyperlink.")
default_link_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat().setHyperlinkClick(Hyperlink("https://www.aspose.com/"))
presentation.save("presentation-out-hyperlink.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Remove Hyperlinks from Presentations
Remove Hyperlinks from Text
This Python code shows you how to remove the hyperlink from text on a presentation slide:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat, AutoShape
presentation = Presentation("presentation.pptx")
try:
slide = presentation.getSlides().get_Item(0)
for shape in slide.getShapes():
if isinstance(shape, AutoShape):
text_frame = shape.getTextFrame()
if text_frame is not None:
for paragraph in text_frame.getParagraphs():
for portion in paragraph.getPortions():
portion.getPortionFormat().getHyperlinkManager().removeHyperlinkClick()
presentation.save("pres-removed-hyperlinks.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Remove Hyperlinks from Shapes or Frames
This Python code shows you how to remove the hyperlink from a shape on a presentation slide:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat
presentation = Presentation("presentation.pptx")
try:
slide = presentation.getSlides().get_Item(0)
for shape in slide.getShapes():
shape.getHyperlinkManager().removeHyperlinkClick()
presentation.save("pres-removed-hyperlinks.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Mutable Hyperlink
The Hyperlink class is mutable. With this class, you can change the values for these properties:
The code snippet shows you how to add a hyperlink to a slide and edit its tooltip later:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Hyperlink, Presentation, SaveFormat, ShapeType
presentation = Presentation()
try:
shape = presentation.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 600, 50, False)
shape.addTextFrame("Aspose: File Format APIs")
portion_format = shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat()
portion_format.setHyperlinkClick(Hyperlink("https://www.aspose.com/"))
portion_format.getHyperlinkClick().setTooltip("More than 70% Fortune 100 companies trust Aspose APIs")
portion_format.setFontHeight(32)
# Changes the tooltip of the hyperlink that has already been added
portion_format.getHyperlinkClick().setTooltip("Aspose: the File Format APIs")
presentation.save("presentation-out.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Supported Properties in HyperlinkQueries
You can access HyperlinkQueries from a presentation, slide, or text for which the hyperlink is defined.
The HyperlinkQueries class supports these methods and properties:
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).