python-docx 超链接 超链接已创建但不可点击

问题描述 投票:0回答:1

我从数据库中获取元组并使用这些值创建一个文档。其中,我需要一个超链接,所以我为它写了一个函数。但是,文档已创建,但当我单击它时它不起作用,因此当文档完成并且我进行额外的换行处理时,它工作正常。请告诉我这个问题!

def add_hyperlink(paragraph,text, url):
    # Create a new hyperlink element
    hyperlink = OxmlElement('w:hyperlink')
    hyperlink.set(qn('w:anchor'), url)  # 하이퍼링크 URL 설정

    # Create ‘w:t’, a sub-element of hyperlink
    t = OxmlElement('w:t')
    t.text = text
    hyperlink.append(t)

    # Show hyperlink text in blue
    run = paragraph.add_run()
    run._r.append(hyperlink)
    run.font.color.rgb = RGBColor(0, 0, 255)

在正文中是这样使用的:

            p1 = doc.add_paragraph(f"file : ", style='Normal')
            add_hyperlink(p1,base_url + path, base_url + path)
            run = p1.runs[0]
            run.add_break(WD_BREAK.LINE)  # 줄바꿈 추가
python django python-docx
1个回答
0
投票

w:anchor
属性仅用于URLfragment,就像
"#summary"
https://blog.com/somepost#summary
部分。

实际 URL 位于 document.xml.rels 文件(部分)中的

Relationship
元素中。

您将需要一个不同的函数来完成这一任务。

这样的事情可能会成功:https://stackoverflow.com/a/47666747/1902513

© www.soinside.com 2019 - 2024. All rights reserved.