[英]Extract hyperlink from pptx
我想從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 時的文本帶有超鏈接。
在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
我對 python 部分無能為力,但這里有一個如何提取超鏈接 URL 本身的示例,而不是應用鏈接的文本,這就是你想要的。
PPT 中的每張幻燈片都有一個超鏈接集合,其中包含幻燈片上的所有超鏈接。 每個超鏈接都有一個.Address 和.SubAddress 屬性。 In the case of a URL like https://www.someplace.com#placeholder , the.Address would be https://www.someplace.com and the.SubAddress would be placeholder, for example.
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.