Python在docx中更改超链接的颜色和样式
我在我的代码中添加了一个Python在docx中更改超链接的颜色和样式,python,python-3.x,docx,python-docx,Python,Python 3.x,Docx,Python Docx,我在我的代码中添加了一个add_hyperlink函数,我可以使用该函数将超链接添加到我的文档中,但当我想更改它们的颜色时,我不能这样做。以下是函数: def add_hyperlink(paragraph, url, text): part = paragraph.part r_id = part.relate_to(url, docx.opc.constants.RELATIONSHIP_TYPE.HYPERLINK, is_external=True
add_hyperlink
函数,我可以使用该函数将超链接添加到我的文档中,但当我想更改它们的颜色时,我不能这样做。以下是函数:
def add_hyperlink(paragraph, url, text):
part = paragraph.part
r_id = part.relate_to(url, docx.opc.constants.RELATIONSHIP_TYPE.HYPERLINK, is_external=True)
hyperlink = docx.oxml.shared.OxmlElement('w:hyperlink')
hyperlink.set(docx.oxml.shared.qn('r:id'), r_id, )
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)
paragraph._p.append(hyperlink)
return hyperlink
我试着用电脑来改变它
hyperl = add_hyperlink(paragraph, 'https://stackoverflow.com', 'stackoverflow')
hyperl.font.color.rgb = RGBColor(0x99, 0x99, 0x99)
但它不起作用。我已经试了好几天了。是否可以从代码更改超链接的颜色或样式?我解决了这个问题。我在函数中的
返回超链接
行顶部添加了这些行
r = paragraph.add_run()
r._r.append (hyperlink)
r.font.color.theme_color = MSO_THEME_COLOR_INDEX.HYPERLINK