Python-docx: Is it possible to add a hyperlink to a paragraph?

Created on 12 Apr 2017  路  7Comments  路  Source: python-openxml/python-docx

How to add a hyperlink to the some text? For example, there is an article titled with "This is the Title". When the title is clicked, I would launch the internet explorer and switch to the website "https://github.com".

How can I manage to do this?

Most helpful comment

I've got it, thank you all.
The following code from #74 is executable:

def add_hyperlink(paragraph, url, text):
"""
A function that places a hyperlink within a paragraph object.

:param paragraph: The paragraph we are adding the hyperlink to.
:param url: A string containing the required url
:param text: The text displayed for the url
:return: The hyperlink object
"""

# 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
new_run = docx.oxml.shared.OxmlElement('w:r')

# Create a new w:rPr element
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)

paragraph._p.append(hyperlink)

return hyperlink

import docx
document = docx.Document()
p = document.add_paragraph()
add_hyperlink(p, 'http://www.baidu.com', 'baidu')
document.save('demo_hyperlink.docx')

All 7 comments

Is it possible to add a hyperlink to a paragraph, just like this:

hyperlink = paragraph.add_hyperlink(text='foobar', url='http://github.com')

Check out #74

I still don't know the answer, how can I manage to add a hyperlink to a paragraph?

Check out #74 as @scanny mentioned. There is some code in the comments that you can use to do this by myself and another.

I'm sorry for I have not found the example or demo application in the comments. Can you do me a favor by showing me the executable example?
I do not know how to run the following example, what is the type of the variable paragraph?
hyperlink = paragraph.add_hyperlink(text='foobar', url='http://github.com')

I've got it, thank you all.
The following code from #74 is executable:

def add_hyperlink(paragraph, url, text):
"""
A function that places a hyperlink within a paragraph object.

:param paragraph: The paragraph we are adding the hyperlink to.
:param url: A string containing the required url
:param text: The text displayed for the url
:return: The hyperlink object
"""

# 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
new_run = docx.oxml.shared.OxmlElement('w:r')

# Create a new w:rPr element
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)

paragraph._p.append(hyperlink)

return hyperlink

import docx
document = docx.Document()
p = document.add_paragraph()
add_hyperlink(p, 'http://www.baidu.com', 'baidu')
document.save('demo_hyperlink.docx')

@scanny I have been searching for a way to extract the text as well as its associated hyperlink from a docx file paragraph by paragraph. Is that possible? Could you please suggest a way to do that. Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kndidox picture kndidox  路  5Comments

floatc picture floatc  路  4Comments

chcourses picture chcourses  路  3Comments

StephenCzarnecki picture StephenCzarnecki  路  4Comments

carlosmtz2106 picture carlosmtz2106  路  7Comments