Umar.H
Umar.H

Reputation: 23099

Removing hyperlinks in PowerPoint with python-pptx

Quite new to XML and the python-pptx module I want to remove a single hyperlink that is present on every page

my own attempt so far has been to retrieve my files, change to zip format and unzip them into separate folders

I then locate the following attribute <a:hlinkClick r:id="RelId4">

and remove it whilst removing the Relationshipattribute within the xml.rels file which corresponds to this slide.

I then rezip and change the extension to pptx and this loads fines. I then tried to replicate this in Python so I can create an on-going automation.

my attempt:

from pathlib import Path
import zipfile as zf
from pptx import Presentation
import re
import xml.etree.ElementTree as ET

path = 'mypath'
ppts = [files for files in Path(path).glob('*.pptx')]
for file in ppts:
    file.rename(file.with_suffix('.zip'))
zip_files = ppts = [files for files in Path(path).glob('*.zip')]

for zips in zip_files:
    with zf.ZipFile(zips,'r') as zip_ref:
        zip_ref.extractall(Path(path).joinpath('zipFiles',zips.stem))

I then do some further filtering and end up with my xmls from the rels folder & the ppt/slide folder.

it's here that I get stuck I can read my xml with the ElementTree Module but I cannot find the relevant tag to remove?

for file in normal_xmls:
    tree = (ET.parse(file).getroot())
    y = tree.findall('a')
    print(y)

this yields nothing, I tried to use the python-pptx module but the .Action.Hyperlink doesn't seem to be a complete feature unless I am misunderstanding the API.

Upvotes: 1

Views: 619

Answers (1)

scanny
scanny

Reputation: 28863

To remove a hyperlink from a shape (the kind where clicking on the shape navigates somewhere), set the hyperlink address to None:

shape.click_action.hyperlink.address = None

Upvotes: 1

Related Questions