python docx库使用样例_使用python docx库将链接添加到MS Word中的书签

1586010002-jmsa.png

I've used the code from an earlier question to create a hyperlink:

Adding an hyperlink in MSWord by using python-docx

I now want to create a link to a bookmark within the document, rather than an external hyperlink, but can't work out how to do it. Any ideas?

解决方案

Never mind. Found a way, thanks to neilbilly at github:

feature: Paragraph.add_hyperlink() #74

def add_link(paragraph, link_to, text):

hyperlink = docx.oxml.shared.OxmlElement('w:hyperlink')

hyperlink.set(docx.oxml.shared.qn('w:anchor'), link_to, )

new_run = docx.oxml.shared.OxmlElement('w:r')

rPr = docx.oxml.shared.OxmlElement('w:rPr')

new_run.append(rPr)

new_run.text = text

hyperlink.append(new_run)

r = paragraph.add_run ()

r._r.append (hyperlink)

r.font.name = "Calibri"

r.font.color.theme_color = MSO_THEME_COLOR_INDEX.HYPERLINK

r.font.underline = True

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值