从 pptx 中提取超链接
Extract hyperlink from pptx
我想从pptx中提取超链接,我知道怎么用word做,但是谁知道如何从pptx中提取超链接?
例如,我在 pptx 中有一段文字,我想得到 url https://whosebug.com/ :
你好,Whosebug
我尝试编写 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 当文本带有超链接时。
我帮不上 python 部分,但这里有一个示例,说明如何提取超链接 URL 本身,而不是链接应用到的文本,这就是以为你在追求。
PPT 中的每张幻灯片都有一个超链接集合,其中包含幻灯片上的所有超链接。每个超链接都有一个 .Address 和 .SubAddress 属性。在 URL 的情况下,例如 https://www.someplace.com#placeholder, the .Address would be 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 Sub
在 python-pptx
中,hyperlink 可以出现在 Run
上,我相信这就是您所追求的。请注意,这意味着零个或多个 hyperlinks 可以出现在给定的形状中。另请注意,hyperlink 也可以出现在整体形状上,这样单击形状就会跟随 link。在这种情况下,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
我想从pptx中提取超链接,我知道怎么用word做,但是谁知道如何从pptx中提取超链接?
例如,我在 pptx 中有一段文字,我想得到 url https://whosebug.com/ :
你好,Whosebug
我尝试编写 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 当文本带有超链接时。
我帮不上 python 部分,但这里有一个示例,说明如何提取超链接 URL 本身,而不是链接应用到的文本,这就是以为你在追求。
PPT 中的每张幻灯片都有一个超链接集合,其中包含幻灯片上的所有超链接。每个超链接都有一个 .Address 和 .SubAddress 属性。在 URL 的情况下,例如 https://www.someplace.com#placeholder, the .Address would be 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 Sub
在 python-pptx
中,hyperlink 可以出现在 Run
上,我相信这就是您所追求的。请注意,这意味着零个或多个 hyperlinks 可以出现在给定的形状中。另请注意,hyperlink 也可以出现在整体形状上,这样单击形状就会跟随 link。在这种情况下,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