-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmy_docx_utils.py
55 lines (43 loc) · 1.92 KB
/
my_docx_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import docx
from docx.opc.constants import RELATIONSHIP_TYPE as RT
from docx.oxml.shared import OxmlElement, qn
from docx.enum.dml import MSO_THEME_COLOR_INDEX
def add_hyperlink(paragraph, url, text, font_name=None,
underline_hyperlink=True, indent=0, color=True):
"""
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: A Run object containing the hyperlink
"""
# 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, RT.HYPERLINK, is_external=True)
# Create the w:hyperlink tag and add needed values
hyperlink = OxmlElement('w:hyperlink')
hyperlink.set(qn('r:id'), r_id, )
hyperlink.set(qn('w:history'), '1')
# Create a w:r element
new_run = OxmlElement('w:r')
# Create a new w:rPr element
rPr = OxmlElement('w:rPr')
# Create a w:rStyle element, note this currently does not add the hyperlink style as its not in
# the default template, I have left it here in case someone uses one that has the style in it
rStyle = OxmlElement('w:rStyle')
rStyle.set(qn('w:val'), 'Hyperlink')
# Join all the xml elements together add add the required text to the w:r element
rPr.append(rStyle)
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.font.name = font_name
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
if color:
r.font.color.theme_color = MSO_THEME_COLOR_INDEX.HYPERLINK
r.font.underline = underline_hyperlink
return r