如何使用 python 访问演示文稿的形状或文本运行中存在的 URL?



我正在使用python从pptx文件中读取数据。我需要访问其中存在的超链接/网址。

ppt2 = Presentation('../sample dataset/'+ file_name)
for slide in ppt2.slides:
for shape in slide.shapes:
click_action = shape.click_action
if click_action.action == PP_ACTION.HYPERLINK:
print(click_action.hyperlink.address)

我试过这个,但它不起作用。 它没有显示任何输出。

我需要超链接中存在的网址作为输出。但我没有得到任何输出。 PPT 幻灯片的外观示例

带有文本"示例文本"的超链接具有 URL。我需要访问网址(请参阅ppt幻灯片图像(。

试试这个

for slide in prs.slides:
for shape in slide.shapes:
if hasattr(shape, "hyperlink"):
hyperlink = shape.hyperlink
hyperlink_address = hyperlink.address
hyperlink_text = ""
if hasattr(shape, "text"):
hyperlink_text = shape.text
print("hyperlink_text", hyperlink_text, "hyperlink_address", hyperlink_address)
elif shape.has_text_frame:
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
if not hasattr(run, "hyperlink"):
continue
hyperlink = run.hyperlink
hyperlink_text = ""
if hasattr(run, "text"):
hyperlink_text = run.text
hyperlink_address = hyperlink.address
if hyperlink_address == None:
continue
print("hyperlink_text", hyperlink_text, "hyperlink_address", hyperlink_address)

最新更新