Adding a link to a bookmark in MS Word using python docx library

五迷三道 提交于 2020-01-22 02:15:23

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/59624970/adding-a-link-to-a-bookmark-in-ms-word-using-python-docx-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!