首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从pptx中提取超链接

从pptx中提取超链接
EN

Stack Overflow用户
提问于 2021-04-22 06:42:20
回答 2查看 457关注 0票数 3

我想从pptx中提取超链接,我知道如何在word中实现,但是有谁知道如何从pptx中提取它呢?

例如,我在pptx中有一个文本,我想得到url https://stackoverflow.com/

你好,stackoverflow

我试图编写Python代码以获得文本:

代码语言:javascript
复制
from pptx import Presentation
from pptx.opc.constants import RELATIONSHIP_TYPE as RT

ppt = Presentation('data/ppt.pptx')

for i, sld in enumerate(ppt.slides, start=1):
    print(f'-- {i} --')
    for shp in sld.shapes:
        if shp.has_text_frame:
            print(shp.text)

但我只想打印文本和URL时,与超链接文本。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-04-22 16:44:37

python-pptx中,超链接可以出现在Run上,我相信这就是您所追求的。请注意,这意味着零或更多的超链接可以出现在给定的形状。还请注意,超链接也可以出现在整体形状上,因此单击该形状会跟随该链接。在这种情况下,URL的文本不会出现。

代码语言:javascript
复制
from pptx import Presentation

prs = Presentation('data/ppt.pptx')

for slide in prs.slides:
    for shape in slide.shapes:
        if not shape.has_text_frame:
            continue
        for paragraph in shape.text_frame.paragraphs:
            for run in paragraph.runs:
                address = run.hyperlink.address
                if address is None:
                    continue
                print(address)

这些文件的相关章节如下:

https://python-pptx.readthedocs.io/en/latest/api/text.html#run-objects

在这里:

https://python-pptx.readthedocs.io/en/latest/api/action.html#hyperlink-objects

票数 2
EN

Stack Overflow用户

发布于 2021-04-22 15:30:52

我无法帮助python部分,但这里有一个示例,说明如何提取超级链接URL本身,而不是应用链接的文本,这就是您想要的结果。

PPT中的每个幻灯片都有一个超链接集合,其中包含幻灯片上的所有超链接。每个超链接都有一个.Address和.SubAddress属性。例如,对于像https://www.someplace.com#placeholder这样的URL,.Address是https://www.someplace.com,.SubAddress是占位符。

代码语言:javascript
复制
Sub ExtractHyperlinks()

Dim oSl As Slide
Dim oHl As Hyperlink
Dim sOutput As String

' Look at each slide in the presentation
For Each oSl In ActivePresentation.Slides
    sOutput = sOutput & "Slide " & oSl.SlideIndex & vbCrLf
    ' Look at each hyperlink on the slide
    For Each oHl In oSl.Hyperlinks
        sOutput = sOutput & vbTab & oHl.Address & " | " & oHl.SubAddress & vbCrLf
    Next    ' Hyperlink
Next    ' Slide

Debug.Print sOutput

End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67208020

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档