我想从pptx中提取超链接,我知道如何在word中实现,但是有谁知道如何从pptx中提取它呢?
例如,我在pptx中有一个文本,我想得到url https://stackoverflow.com/:
我试图编写Python代码以获得文本:
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时,与超链接文本。
发布于 2021-04-22 16:44:37
在python-pptx中,超链接可以出现在Run上,我相信这就是您所追求的。请注意,这意味着零或更多的超链接可以出现在给定的形状。还请注意,超链接也可以出现在整体形状上,因此单击该形状会跟随该链接。在这种情况下,URL的文本不会出现。
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
发布于 2021-04-22 15:30:52
我无法帮助python部分,但这里有一个示例,说明如何提取超级链接URL本身,而不是应用链接的文本,这就是您想要的结果。
PPT中的每个幻灯片都有一个超链接集合,其中包含幻灯片上的所有超链接。每个超链接都有一个.Address和.SubAddress属性。例如,对于像https://www.someplace.com#placeholder这样的URL,.Address是https://www.someplace.com,.SubAddress是占位符。
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 Subhttps://stackoverflow.com/questions/67208020
复制相似问题