如何在pptx python上向表格单元格中的文本添加超链接?

2024-05-15 15:39:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用pptx python创建一个报告。 我正在尝试向其中一个表格单元格添加超链接。 有人知道我怎么做吗? 这是我的代码:

prs = Presentation()
prs.slide_width = Inches(16)
prs.slide_height = Inches(9)

title_only_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(title_only_slide_layout)
shapes = slide.shapes

rows = 2
cols = 1
left = Inches(0.5)
top = Inches(1.5)
width = Inches(15)
height = Inches(0.8)
table = shapes.add_table(rows, cols, left, top, width, height).table

table.cell(0,0).text = 'title'
table.cell(1,0).text = 'link'

我希望链接文本是超链接。 我将感谢您的帮助:)


Tags: addonlytitletoptablewidthleftrows
1条回答
网友
1楼 · 发布于 2024-05-15 15:39:16

添加超链接有点棘手。您首先需要在文本中添加“run”,添加要显示的文本,然后添加超链接地址。例如:

cell = table.cell(0,1)
cell_link = cell.text_frame.paragraphs[0].add_run()
cell_link.text = "Text to display"
hlink = cell_link.hyperlink
hlink.address = "https://www.somwehere.com"

相关问题 更多 >