使用python docx库将链接添加到MS Word中的书签 [英] Adding a link to a bookmark in MS Word using python docx library

查看:870
本文介绍了使用python docx库将链接添加到MS Word中的书签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了先前问题中的代码来创建超链接: 使用python-docx在MSWord中添加超链接

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?

推荐答案

没关系.找到了一种方法,多亏了github上的neilbilly: 功能:Paragraph.add_hyperlink()#74 def add_link(段落,link_to,文本): 超链接= docx.oxml.shared.OxmlElement('w:hyperlink') 超链接集(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 =文字 超链接.附加(new_run) r =段落.add_run() r._r.append(超链接) r.font.name ="Calibri" r.font.color.theme_color = MSO_THEME_COLOR_INDEX.HYPERLINK r.font.underline = True

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

这篇关于使用python docx库将链接添加到MS Word中的书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆