Python: Remove Hyperlinks from PowerPoint Presentations

2024-08-21 00:51:20 Written by  support iceblue
Rate this item
(0 votes)

PowerPoint presentations often contain hyperlinks that guide audiences to additional resources or locations within the presentations. While these links can be useful for providing further information and easy navigation, there are instances where they may detract from the presentation's flow or compromise its professional appearance. Those invalid or unnecessary links in slides can be easily removed using Python, enhancing the overall quality of the presentations.

This article will show how Spire.Presentation for Python can be utilized to remove hyperlinks from PowerPoint presentations efficiently.

Install Spire.Presentation for Python

This scenario requires Spire.Presentation for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.Presentation

If you are unsure how to install, please refer to: How to Install Spire.Presentation for Python on Windows

Remove Hyperlinks from Text in PowerPoint Slides

The normal text in a PowerPoint presentation is contained in auto shapes. Developers can access the text ranges within these shapes using the IAutoShape.TextFrame.Paragraphs[].TextRanges[] property and read or set the hyperlinks on them using the TextRange.ClickAction property. Hyperlinks on text can be removed by setting the TextRange.ClickAction property to None.

The detailed steps are as follows:

  • Create an instance of Presentation class and load a PowerPoint presentation using Presentation.LoadFromFile() method.
  • Iterate through the slides in the presentation and then iterate through the shapes in the slides.
  • Check if the shape is an instance of IAutoShape. If it is, iterate through the paragraphs in the shape, and then the text ranges in the paragraphs.
  • Check if the TextRange.ClickAction property of a text range is None. If it is not, remove the hyperlink by setting TextRange.ClickAction property to None.
  • Save the presentation using Presentation.SaveToFile() method.
  • Python
from spire.presentation import Presentation, IAutoShape, FileFormat

# Create an instance of Presentation
presentation = Presentation()

# Load a PowerPoint presentation
presentation.LoadFromFile("Sample.pptx")

# Iterate through the slides in the presentation
for slide in presentation.Slides:
    # Iterate through the shapes
    for shape in slide.Shapes:
        # Check if the shape is an AutoShape instance
        if isinstance(shape, IAutoShape):
            # Iterate through the paragraphs in the shape
            for paragraph in shape.TextFrame.Paragraphs:
                # Iterate through the text ranges in the paragraph
                for textRange in paragraph.TextRanges:
                    # Check if the text range has a hyperlink
                    if textRange.ClickAction is not None:
                        # Remove the hyperlink
                        textRange.ClickAction = None

# Save the presentation
presentation.SaveToFile("output/RemoveSlideTextHyperlink.pptx", FileFormat.Pptx2013)
presentation.Dispose()

Python: Remove Hyperlinks from PowerPoint Presentations

Remove Hyperlinks from All Shapes in PowerPoint Slides

The IShape class represents all types of shapes in a presentation slide, such as auto shapes, images, tables, and more. The hyperlink on all these shapes can be removed by setting the value obtained from the IShape.Click.get_NoAction() method as the value of the shapes’ IShape.Click property. The detailed steps are as follows:

  • Create an instance of Presentation class and load a PowerPoint presentation using Presentation.LoadFromFile() method.
  • Iterate through the slides in the presentation and then iterate through the shapes in the slides.
  • Check if the IShape.Click property is None. If it is not, remove the hyperlink by setting the property to the result of IShape.Click.get_NoAction() method.
  • Save the presentation using Presentation.SaveToFile() method.
  • Python
from spire.presentation import Presentation, FileFormat

# Create an instance of Presentation
presentation = Presentation()

# Load a PowerPoint presentation
presentation.LoadFromFile("Sample.pptx")

# Iterate through the slides in the presentation
for slide in presentation.Slides:
    # Iterate through the shapes in the slide
    for shape in slide.Shapes:
        # Check if the shape has a hyperlink
        if shape.Click is not None:
            # Remove the click action
            shape.Click = shape.Click.get_NoAction()

# Save the presentation
presentation.SaveToFile("output/RemoveSlideShapeHyperlink.pptx", FileFormat.Pptx2013)
presentation.Dispose()

Python: Remove Hyperlinks from PowerPoint Presentations

Remove Hyperlinks from Specific Types of Shapes in PowerPoint Slides

In addition to directly removing hyperlinks for all shapes, we can also determine the shape type before removing the hyperlinks to find and remove hyperlinks from shapes of the specified type. The detailed steps are as follows:

  • Create an instance of Presentation class and load a PowerPoint presentation using Presentation.LoadFromFile() method.
  • Iterate through the slides in the presentation and then iterate through the shapes in the slides.
  • Check if the shape is an instance of IEmbedImage, ITable, or IChart. If it is, check if the IShape.Click property of the shape is None. If it is not, remove the hyperlink by setting the property to the result of IShape.Click.get_NoAction() method.
  • Save the presentation using Presentation.SaveToFile() method.
  • Python
from spire.presentation import Presentation, FileFormat, IEmbedImage, ITable, IChart

# Create an instance of Presentation
presentation = Presentation()

# Load a PowerPoint presentation
presentation.LoadFromFile("Sample.pptx")

# Iterate through the slides in the presentation
for slide in presentation.Slides:
    # Iterate through the shapes in the slide
    for shape in slide.Shapes:
        # Check if the shape is an embedded image
        if isinstance(shape, (IEmbedImage, ITable, IChart)):
            # check if the click action is not None
            if shape.Click is not None:
                # Remove the click action
                shape.Click = shape.Click.get_NoAction()

# Save the presentation
presentation.SaveToFile("output/RemoveSlideShapeTypeHyperlink.pptx", FileFormat.Pptx2013)
presentation.Dispose()

Python: Remove Hyperlinks from PowerPoint Presentations

Remove Hyperlinks from Table Text in PowerPoint Slides

To remove hyperlinks from text within a table, it is necessary to iterate through the table's cells and the text ranges within each cell. Afterward, the hyperlinks on the text ranges in each cell can be removed by setting the TextRange.ClickAction property to None. The detailed steps are as follows:

  • Create an instance of Presentation class and load a PowerPoint presentation using Presentation.LoadFromFile() method.
  • Iterate through the slides in the presentation and then iterate through the shapes in the slides.
  • Check if a shape is an instance of ITable class. If it is, iterate through the rows in the table and then the cells in the rows.
  • Iterate through the paragraphs in the cells and then the text ranges in the paragraphs.
  • Check if the TextRange.ClickAction property of a text range is None. If it is not, remove the hyperlink by setting the value of the property to None.
  • Save the presentation using Presentation.SaveToFile() method.
  • Python
from spire.presentation import Presentation, ITable, FileFormat

# Create an instance of Presentation
presentation = Presentation()

# Load a PowerPoint presentation
presentation.LoadFromFile("Sample.pptx")

# Iterate through the slides in the presentation
for slide in presentation.Slides:
    # Iterate through the shapes in the slide
    for shape in slide.Shapes:
        # Check if the shape is a table
        if isinstance(shape, ITable):
            # Get the table
            table = ITable(shape)
            # Iterate through the rows in the table
            for row in table.TableRows:
                # Iterate through the cells in the row
                for cell in row:
                    # Iterate through the paragraphs in the cell
                    for para in cell.TextFrame.Paragraphs:
                        # Iterate through the text ranges in the paragraph
                        for range in para.TextRanges:
                            # Check if the text run contains a hyperlink
                            if range.ClickAction is not None:
                                # Remove the hyperlink
                                range.ClickAction = None

presentation.SaveToFile("output/RemoveSlideTableTextHyperlink.pptx", FileFormat.Pptx2013)
presentation.Dispose()

Python: Remove Hyperlinks from PowerPoint Presentations

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Wednesday, 21 August 2024 07:24