2
2
Hyperlink
3
3
=========
4
4
5
- Word allows hyperlinks to be placed in the document or existing objects to be
6
- turned into hyperlinks.
5
+ Word allows hyperlinks to be placed in the document.
7
6
8
- Hyperlinks can point to a named object or range within the current document or
9
- to an external resource. Hyperlinks can contain multiple runs of text.
7
+ Hyperlink may link to a external location, for example, as an url. It may link to
8
+ a location within the document, for example, as an bookmark. These two cases are
9
+ handled differently. Until today (03/2016), we support only external links.
10
10
11
+ Hyperlinks can contain multiple runs of text.
11
12
12
13
Candidate protocol
13
14
------------------
14
15
16
+ The hyperlink feature supports only external links.
17
+
15
18
Add a simple hyperlink with text and url:
16
19
17
- >>> hyperlink = paragraph.add_hyperlink(text = ' Google' , url = ' http://google.com' )
20
+ >>> hyperlink = paragraph.add_hyperlink(text = ' Google' , address = ' https://google.com' )
21
+ >>> hyperlink
22
+ <docx.text.hyperlink.Hyperlink at 0x7f...>
18
23
>>> hyperlink.text
19
24
'Google'
20
- >>> hyperlink.url
21
- 'http://google.com'
22
- >>> hyperlink.anchor
23
- None
24
- >>> len (hyperlink.runs)
25
- 1
25
+ >>> hyperlink.address
26
+ 'https://google.com'
27
+ >>> hyperlink.runs
28
+ [<docx.text.run.Run at 0x7f...>]
26
29
27
30
Add multiple runs to a hyperlink:
28
31
29
- >>> hyperlink = paragraph.add_hyperlink(url = ' http ://github.com' )
32
+ >>> hyperlink = paragraph.add_hyperlink(address = ' https ://github.com' )
30
33
>>> hyperlink.add_run(' A' )
31
34
>>> hyperlink.add_run(' formatted' ).italic = True
32
35
>>> hyperlink.add_run(' link' ).bold = True
33
- >>> len ( hyperlink.runs)
34
- 3
35
-
36
- Add a hyperlink pointing to a named range in the current document:
36
+ >>> hyperlink.runs
37
+ [<docx.text.run.Run at 0x7f...>,
38
+ <docx.text.run.Run at 0x7fb...>,
39
+ <docx.text.run.Run at 0x7fb...>]
37
40
38
- >>> hyperlink = paragraph.add_hyperlink(text = ' Section 1' , anchor = ' section1' )
39
- >>> hyperlink.anchor
40
- 'section1'
41
- >>> hyperlink.url
42
- None
41
+ Retrieve a paragraph's content:
43
42
44
- Turning an existing object into a hyperlink:
43
+ >>> paragraph = document.add_paragraph(' A plain paragraph having some ' )
44
+ >>> paragraph.add_run(' link such as ' )
45
+ >>> paragraph.add_hyperlink(address = ' http://github.com' , text = ' github' )
46
+ >>> paragraph.iter_p_content():
47
+ [<docx.text.paragraph.Run at 0x7f...>,
48
+ <docx.text.paragraph.Hyperlink at 0x7f...>]
45
49
46
- >>> existing_object = document.add_paragraph(' Some text' )
47
- >>> hyperlink = existing_object.hyperlink(url = ' http://google.com' )
48
- >>> hyperlink.text
49
- 'Some text'
50
- >>> len (hyperlink.runs)
51
- 1
52
50
53
51
54
52
Specimen XML
55
53
------------
56
54
57
55
.. highlight :: xml
58
56
59
- A simple hyperlink to an external url::
60
57
61
- <w:hyperlink r:id="rId9">
62
- <w:r>
63
- <w:rPr>
64
- <w:rStyle w:val="Hyperlink"/>
65
- </w:rPr>
66
- <w:t>Google</w:t>
67
- </w:r>
68
- </w:hyperlink>
58
+ External links
59
+ ~~~~~~~~~~~~~~
69
60
61
+ A simple hyperlink to an external url::
70
62
71
- The relationship for the above url::
72
-
73
- <Relationships xmlns="…">
74
- <Relationship Id="rId9" Mode="External" Target=http://google.com />
75
- </Relationships>
76
-
77
- A hyperlink to an internal named range::
78
-
79
- <w:hyperlink r:anchor="section1">
63
+ <w:p>
80
64
<w:r>
81
- <w:rPr>
82
- <w:rStyle w:val="Hyperlink"/>
83
- </w:rPr>
84
- <w:t>Google</w:t>
65
+ <w:t xml:space="preserve">This is an external link to </w:t>
85
66
</w:r>
86
- </w:hyperlink>
67
+ <w:hyperlink r:id="rId4">
68
+ <w:r>
69
+ <w:rPr>
70
+ <w:rStyle w:val="Hyperlink"/>
71
+ </w:rPr>
72
+ <w:t>Google</w:t>
73
+ </w:r>
74
+ </w:hyperlink>
75
+ </w:p>
76
+
77
+
78
+ The r:id="rId4" references the following relationship within the relationships
79
+ part for the document document.xml.rels.::
80
+
81
+ <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
82
+ <Relationship Id="rId4" Mode="External"
83
+ Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
84
+ Target="http://google.com/"/>
85
+ </Relationships>
87
86
88
87
A hyperlink with multiple runs of text::
89
88
@@ -99,66 +98,99 @@ A hyperlink with multiple runs of text::
99
98
<w:rStyle w:val="Hyperlink"/>
100
99
<w:i/>
101
100
</w:rPr>
102
- <w:t xml:space="preserve">formatted</w:t>
101
+ <w:t xml:space="preserve"> formatted</w:t>
103
102
</w:r>
104
103
<w:r>
105
104
<w:rPr>
106
105
<w:rStyle w:val="Hyperlink"/>
107
106
<w:b/>
108
107
</w:rPr>
109
- <w:t xml:space="preserve">link</w:t>
108
+ <w:t xml:space="preserve"> link</w:t>
110
109
</w:r>
111
110
</w:hyperlink>
112
111
113
112
114
- Resources
115
- ---------
116
-
117
- * `Document Members (Word) on MSDN `_
118
- * `Hyperlink Members (Word) on MSDN `_
119
- * `Hyperlinks Members (Word) on MSDN `_
120
- * `Hyperlink Class (OpenXML.Office2010.CustomUI) on MSDN `_
121
- * `Hyperlink Class (OpenXML.Wordprocessing) on MSDN `_
122
-
123
-
124
- .. _Document Members (Word) on MSDN :
125
- http://msdn.microsoft.com/en-us/library/office/ff840898.aspx
126
-
127
- .. _Hyperlink Members (Word) on MSDN :
128
- http://msdn.microsoft.com/en-us/library/office/ff195109.aspx
129
-
130
- .. _Hyperlinks Members (Word) on MSDN :
131
- http://msdn.microsoft.com/en-us/library/office/ff192421.aspx
113
+ Internal links
114
+ ~~~~~~~~~~~~~~
132
115
133
- .. _Hyperlink Class (OpenXML.Office2010.CustomUI) on MSDN :
134
- http://msdn.microsoft.com/en-us/library/documentformat.openxml.office2010.customui.hyperlink.aspx
116
+ **Note that internal links are not supported. **
135
117
136
- .. _ Hyperlink Class (OpenXML.Wordprocessing) on MSDN :
137
- http://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.hyperlink.aspx
118
+ An internal link, that link to a location in the document, is specified by the anchor attribute.
119
+ The value of the anchor attribute is the name of a bookmark in the document.
138
120
121
+ Example::
139
122
140
- MS API
141
- ------
142
-
143
- The Hyperlinks property on Document holds references to hyperlink
144
- objects in the MS API.
123
+ <w:p>
124
+ <w:r>
125
+ <w:t xml:space="preserve">This is an </w:t>
126
+ </w:r>
127
+ <w:hyperlink w:anchor="myAnchor">
128
+ <w:r>
129
+ <w:rPr>
130
+ <w:rStyle w:val="Hyperlink"/>
131
+ </w:rPr>
132
+ <w:t>internal link</w:t>
133
+ </w:r>
134
+ </w:hyperlink>
135
+ </w:p>
136
+
137
+ ...
138
+
139
+ <w:p>
140
+ <w:r>
141
+ <w:t xml:space="preserve">This is text with a </w:t>
142
+ </w:r>
143
+ <w:bookmarkStart w:id="0" w:name="myAnchor"/>
144
+ <w:r>
145
+ <w:t>bookmark</w:t>
146
+ </w:r>
147
+ <w:bookmarkEnd w:id="0"/>
148
+ </w:p>
145
149
146
- Hyperlinks contain the following properties:
147
150
148
- * Address
149
- * SubAddress
150
- * EmailSubject
151
- * ExtraInfoRequired
152
- * Range (In python-docx this would be the runs inside the hyperlink)
153
- * ScreenTip
154
- * Shape
155
- * Target (where to open the hyperlink. e.g. "_blank", "_left", "_top", "_self", "_parent" etc)
156
- * TextToDisplay
157
- * Type (msoHyperlinkRange, msoHyperlinkShape or msoHyperlinkInlineShape)
151
+ Schema excerpt
152
+ --------------
158
153
154
+ .. highlight :: xml
159
155
160
- Spec references
161
- ---------------
156
+ ::
157
+
158
+ <xsd:complexType name="CT_Hyperlink">
159
+ <xsd:group ref="EG_PContent" minOccurs="0" maxOccurs="unbounded"/>
160
+ <xsd:attribute name="tgtFrame" type="s:ST_String" use="optional"/>
161
+ <xsd:attribute name="tooltip" type="s:ST_String" use="optional"/>
162
+ <xsd:attribute name="docLocation" type="s:ST_String" use="optional"/>
163
+ <xsd:attribute name="history" type="s:ST_OnOff" use="optional"/>
164
+ <xsd:attribute name="anchor" type="s:ST_String" use="optional"/>
165
+ <xsd:attribute ref="r:id"/>
166
+ </xsd:complexType>
167
+
168
+ <xsd:group name="EG_PContent"> <!-- denormalized -->
169
+ <xsd:choice>
170
+ <xsd:element name="r" type="CT_R"/>
171
+ <xsd:element name="hyperlink" type="CT_Hyperlink"/>
172
+ <xsd:element name="fldSimple" type="CT_SimpleField"/>
173
+ <xsd:element name="sdt" type="CT_SdtRun"/>
174
+ <xsd:element name="customXml" type="CT_CustomXmlRun"/>
175
+ <xsd:element name="smartTag" type="CT_SmartTagRun"/>
176
+ <xsd:element name="dir" type="CT_DirContentRun"/>
177
+ <xsd:element name="bdo" type="CT_BdoContentRun"/>
178
+ <xsd:element name="subDoc" type="CT_Rel"/>
179
+ <xsd:group ref="EG_RunLevelElts"/>
180
+ </xsd:choice>
181
+ </xsd:group>
182
+
183
+ <xsd:complexType name="CT_R">
184
+ <xsd:sequence>
185
+ <xsd:group ref="EG_RPr" minOccurs="0"/>
186
+ <xsd:group ref="EG_RunInnerContent" minOccurs="0" maxOccurs="unbounded"/>
187
+ </xsd:sequence>
188
+ <xsd:attribute name="rsidRPr" type="ST_LongHexNumber"/>
189
+ <xsd:attribute name="rsidDel" type="ST_LongHexNumber"/>
190
+ <xsd:attribute name="rsidR" type="ST_LongHexNumber"/>
191
+ </xsd:complexType>
192
+
193
+ <xsd:simpleType name="ST_RelationshipId">
194
+ <xsd:restriction base="xsd:string"/>
195
+ </xsd:simpleType>
162
196
163
- * 17.16.17 hyperlink (Hyperlink)
164
- * 2.3.61 CT_Hyperlink
0 commit comments