Removing hyperlinks in PowerPoint with python-pptx

耗尽温柔 提交于 2020-01-24 19:31:34

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/59846721/removing-hyperlinks-in-powerpoint-with-python-pptx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!