栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用python-docx在MSWord中添加超链接

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用python-docx在MSWord中添加超链接

是的,我们可以做到。参考

import docxfrom docx.enum.dml import MSO_THEME_COLOR_INDEXdef add_hyperlink(paragraph, text, url):    # This gets access to the document.xml.rels file and gets a new relation id value    part = paragraph.part    r_id = part.relate_to(url, docx.opc.constants.RELATIONSHIP_TYPE.HYPERlink, is_external=True)    # Create the w:hyperlink tag and add needed values    hyperlink = docx.oxml.shared.OxmlElement('w:hyperlink')    hyperlink.set(docx.oxml.shared.qn('r:id'), r_id, )    # Create a w:r element and a new w:rPr element    new_run = docx.oxml.shared.OxmlElement('w:r')    rPr = docx.oxml.shared.OxmlElement('w:rPr')    # Join all the xml elements together add add the required text to the w:r element    new_run.append(rPr)    new_run.text = text    hyperlink.append(new_run)    # Create a new Run object and add the hyperlink into it    r = paragraph.add_run ()    r._r.append (hyperlink)    # A workaround for the lack of a hyperlink style (doesn't go purple after using the link)    # Delete this if using a template that has the hyperlink style in it    r.font.color.theme_color = MSO_THEME_COLOR_INDEX.HYPERlink    r.font.underline = True    return hyperlinkdocument = docx.document()p = document.add_paragraph('A plain paragraph having some ')add_hyperlink(p, 'link to my site', "http://supersitedelamortquitue.fr")document.save('demo_hyperlink.docx')


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/641425.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号