简体   繁体   English

使用 python-pptx 删除 PowerPoint 中的超链接

[英]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对 XML 和 python-pptx 模块非常陌生我想删除每个页面上存在的单个超链接

my own attempt so far has been to retrieve my files, change to zip format and unzip them into separate folders到目前为止,我自己的尝试是检索我的文件,更改为zip格式并将它们解压缩到单独的文件夹中

I then locate the following attribute <a:hlinkClick r:id="RelId4">然后我找到以下属性<a:hlinkClick r:id="RelId4">

and remove it whilst removing the Relationship attribute within the xml.rels file which corresponds to this slide.并删除它,同时删除与此幻灯片对应的xml.rels文件中的Relationship属性。

I then rezip and change the extension to pptx and this loads fines.然后我重新压缩并将扩展名更改为 pptx 并且这会加载罚款。 I then tried to replicate this in Python so I can create an on-going automation.然后我尝试在 Python 中复制它,以便我可以创建一个持续的自动化。

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.然后我做一些进一步的过滤,最终得到来自 rels 文件夹和 ppt/slide 文件夹的 xmls。

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?正是在这里我卡住了我可以使用ElementTree模块读取我的 xml,但我找不到要删除的相关标签?

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.这没有任何结果,我尝试使用 python-pptx 模块,但.Action.Hyperlink似乎不是一个完整的功能,除非我误解了 API。

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

shape.click_action.hyperlink.address = None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM