Pro Guide: How to Insert a Hyperlink in PowerPoint Using Python

Pro Guide: How to Insert a Hyperlink in PowerPoint Using Python

·

4 min read

PowerPoint allows you to add hyperlinks to enrich content, improve audience engagement, and enhance understanding of your ideas. You can link to web addresses, specific slides, or multimedia resources like images and videos. In this guide, we’ll show you how to insert a hyperlink in PowerPoint using Python, covering both text and image links. Let’s automate this task with code and save you time and effort together!


When it comes to Python libraries for PowerPoint presentations, you might think of options like Python-pptx, Spire.Presentation for Python, Aspose.Slides for Python, and odin-slides. Among these, Spire.Presentation stands out due to its intuitive APIs, frequent updates, and reliable technical support. In this guide, we'll demonstrate the steps using Spire.Presentation. Don't worry—though the APIs may differ, the overall steps are quite similar.

Hyperlinks can transform a PowerPoint presentation from a static slide show into an engaging experience. For instance, if you need to explain a complex point but the text is too long or boring, linking to a relevant video could be a better option. In this section, we’ll demonstrate how to use the TextRange.ClickAction.Address property in Spire.Presentation to add hyperlinks to text, bringing your slides to life.

Steps to insert hyperlinks to text in PowerPoint slides:

  • Create an object of the Presentation class.

  • Add a shape to the specified slide using the Slides.Shapes.AppendShape() method.

  • Add some text to the slide through the TextParagraph.TextRanges.Append() method.

  • Create a new TextRange instance to represent a text range, and set the hyperlink to it with the TextRange.ClickAction.Address property.

  • Save the PowerPoint presentation to the local storage using the Presentatiom.SaveToFile() method.

Here is the code example of inserting a hyperlink to text on the first slide of a PowerPoint file:

from spire.presentation.common import *
from spire.presentation import *
import math

# Create a new PowerPoint presentation
presentation = Presentation()

# Add a new shape to the first slide
shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, RectangleF.FromLTRB(80, 250, 650, 400))
shape.Fill.FillType = FillFormatType.none
shape.ShapeStyle.LineColor.Color = Color.get_White()

# Add some paragraphs to the shape
para1 = TextParagraph()
tr = TextRange("Spire.Presentation for Python")
tr.Fill.FillType = FillFormatType.Solid
tr.Fill.SolidColor.Color = Color.get_Black()
para1.TextRanges.Append(tr)
para1.Alignment = TextAlignmentType.Left
shape.TextFrame.Paragraphs.Append(para1)
shape.TextFrame.Paragraphs.Append(TextParagraph())

para2 = TextParagraph()
tr1 = TextRange("This is a PowerPoint presentation created to show how to add hyperlinks to PowerPoint slides")
tr1.Fill.FillType = FillFormatType.Solid
tr1.Fill.SolidColor.Color = Color.get_Black()
para2.TextRanges.Append(tr1)
shape.TextFrame.Paragraphs.Append(para2)
shape.TextFrame.Paragraphs.Append(TextParagraph())

# Add text with a hyperlink
para3 = TextParagraph()
tr2 = TextRange("Click to know more about Spire.Presentation for Python.")
tr2.ClickAction.Address = "https://www.e-iceblue.com/Introduce/presentation-for-python.html"
para3.TextRanges.Append(tr2)
shape.TextFrame.Paragraphs.Append(para3)
shape.TextFrame.Paragraphs.Append(TextParagraph())

# Save the result file
presentation.SaveToFile("/AddHyperlinkToText.pptx", FileFormat.Pptx2010)

presentation.Dispose()

How to Insert a Hyperlink to Text in PowerPoint Slides

💡
Note: If you want to link to local documents instead of a website, you only need to change the web link into a file path when using the TextRange.ClickAction.Address property. 

If you’ve already learned how to insert hyperlinks into text in PowerPoint, it’s time to take your presentations a step further by adding hyperlinks to images.

Hyperlinking an image can be a great way to enhance interactivity. For instance, imagine you want to link a company logo to its official webpage. In this section, we will show you how to embed hyperlinks to images in PowerPoint using Python, offering a seamless way to enrich your slides with interactive content.

Steps to insert hyperlinks to images in PowerPoint slides:

  • Create an instance of the Presentation class, and use the Presentation.LoadFromFile() method to read a PowerPoint document.

  • Get a slide with the Presentation.Slides[] property.

  • Insert a rectangle and set its size.

  • Add an image to the rectangle using the ISlide.Shapes.AppendEmbedImageByPath() method.

  • Create a ClickHyperlink object, and embed a hyperlink to it through the IEmbedImage.Click property.

  • Save the resulting PowerPoint presentation by calling the Presentatiom.SaveToFile() method.

Below is an example of attaching a hyperlink to an image on the first slide in a PowerPoint file:

from spire.presentation.common import *
from spire.presentation import *

# Create a new PowerPoint presentation
presentation = Presentation()

# Load a sample file from disk
presentation.LoadFromFile("/AddHyperlinkToText.pptx")

# Get the first slide
slide = presentation.Slides[0]

# Add an image to this slide
rect = RectangleF.FromLTRB(80, 80, 360, 160)
image = slide.Shapes.AppendEmbedImageByPath(ShapeType.Rectangle, "/Logo1.png", rect)

# Add a hyperlink to the image
hyperlink = ClickHyperlink("https://www.e-iceblue.com/Introduce/presentation-for-python.html")
image.Click = hyperlink

# Save the result file
presentation.SaveToFile("/AddHyperlinkToImage.pptx", FileFormat.Pptx2013)

presentation.Dispose()

How to Insert a Hyperlink to a Picture in PowerPoint Presentation

The Conclusion

This guide covers how to insert hyperlinks into PowerPoint presentations, including links for both text and images. Each section provides clear steps along with code examples for easy reference. By the end of this article, adding hyperlinks to your slides will be a breeze. We hope you find this guide useful!