使用 python-pptx 删除 PowerPoint 中的超链接 [英] Removing hyperlinks in PowerPoint with python-pptx

查看:122
本文介绍了使用 python-pptx 删除 PowerPoint 中的超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对 XML 和 python-pptx 模块非常陌生,我想删除每个页面上存在的单个超链接

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

到目前为止,我自己的尝试是检索我的文件,更改为 zip 格式并将它们解压缩到单独的文件夹中

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">

并删除它,同时删除与此幻灯片对应的 xml.rels 文件中的 Relationship 属性.

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

然后我重新压缩并将扩展名更改为 pptx,这会加载罚款.然后我尝试在 Python 中复制它,以便我可以创建一个持续的自动化.

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.

我的尝试:

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))

然后我做一些进一步的过滤,最终得到来自 rels 文件夹的 xmls &ppt/slide 文件夹.

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

我在这里卡住了我可以使用 ElementTree 模块读取我的 xml,但我找不到要删除的相关标签?

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)

这不会产生任何结果,我尝试使用 python-pptx 模块但 .Action.Hyperlink 似乎不是一个完整的功能,除非我误解了 API.

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.

推荐答案

要从形状(单击形状导航到某处的那种)中删除超链接,请将超链接地址设置为 None:

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

这篇关于使用 python-pptx 删除 PowerPoint 中的超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆