|
| 1 | + |
| 2 | +Hyperlink |
| 3 | +========= |
| 4 | + |
| 5 | +Word allows hyperlinks to be placed in the document or existing objects to be |
| 6 | +turned into hyperlinks. |
| 7 | + |
| 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. |
| 10 | + |
| 11 | + |
| 12 | +Candidate protocol |
| 13 | +------------------ |
| 14 | + |
| 15 | +Add a simple hyperlink with text and url: |
| 16 | + |
| 17 | + >>> hyperlink = paragraph.add_hyperlink(text='Google', url='http://google.com') |
| 18 | + >>> hyperlink.text |
| 19 | + 'Google' |
| 20 | + >>> hyperlink.url |
| 21 | + 'http://google.com' |
| 22 | + >>> hyperlink.anchor |
| 23 | + None |
| 24 | + >>> len(hyperlink.runs) |
| 25 | + 1 |
| 26 | + |
| 27 | +Add multiple runs to a hyperlink: |
| 28 | + |
| 29 | + >>> hyperlink = paragraph.add_hyperlink(url='http://github.com') |
| 30 | + >>> hyperlink.add_run('A') |
| 31 | + >>> hyperlink.add_run('formatted').italic = True |
| 32 | + >>> 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: |
| 37 | + |
| 38 | + >>> hyperlink = paragraph.add_hyperlink(text='Section 1', anchor='section1') |
| 39 | + >>> hyperlink.anchor |
| 40 | + 'section1' |
| 41 | + >>> hyperlink.url |
| 42 | + None |
| 43 | + |
| 44 | +Turning an existing object into a hyperlink: |
| 45 | + |
| 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 | + |
| 53 | + |
| 54 | +Specimen XML |
| 55 | +------------ |
| 56 | + |
| 57 | +.. highlight:: xml |
| 58 | + |
| 59 | +A simple hyperlink to an external url:: |
| 60 | + |
| 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> |
| 69 | + |
| 70 | + |
| 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"> |
| 80 | + <w:r> |
| 81 | + <w:rPr> |
| 82 | + <w:rStyle w:val="Hyperlink"/> |
| 83 | + </w:rPr> |
| 84 | + <w:t>Google</w:t> |
| 85 | + </w:r> |
| 86 | + </w:hyperlink> |
| 87 | + |
| 88 | +A hyperlink with multiple runs of text:: |
| 89 | + |
| 90 | + <w:hyperlink r:id="rId2"> |
| 91 | + <w:r> |
| 92 | + <w:rPr> |
| 93 | + <w:rStyle w:val="Hyperlink"/> |
| 94 | + </w:rPr> |
| 95 | + <w:t>A</w:t> |
| 96 | + </w:r> |
| 97 | + <w:r> |
| 98 | + <w:rPr> |
| 99 | + <w:rStyle w:val="Hyperlink"/> |
| 100 | + <w:i/> |
| 101 | + </w:rPr> |
| 102 | + <w:t xml:space="preserve">formatted</w:t> |
| 103 | + </w:r> |
| 104 | + <w:r> |
| 105 | + <w:rPr> |
| 106 | + <w:rStyle w:val="Hyperlink"/> |
| 107 | + <w:b/> |
| 108 | + </w:rPr> |
| 109 | + <w:t xml:space="preserve">link</w:t> |
| 110 | + </w:r> |
| 111 | + </w:hyperlink> |
| 112 | + |
| 113 | + |
| 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 |
| 132 | + |
| 133 | +.. _Hyperlink Class (OpenXML.Office2010.CustomUI) on MSDN: |
| 134 | + http://msdn.microsoft.com/en-us/library/documentformat.openxml.office2010.customui.hyperlink.aspx |
| 135 | + |
| 136 | +.. _Hyperlink Class (OpenXML.Wordprocessing) on MSDN: |
| 137 | + http://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.hyperlink.aspx |
| 138 | + |
| 139 | + |
| 140 | +MS API |
| 141 | +------ |
| 142 | + |
| 143 | +The Hyperlinks property on Document holds references to hyperlink |
| 144 | +objects in the MS API. |
| 145 | + |
| 146 | +Hyperlinks contain the following properties: |
| 147 | + |
| 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) |
| 158 | + |
| 159 | + |
| 160 | +Spec references |
| 161 | +--------------- |
| 162 | + |
| 163 | +* 17.16.17 hyperlink (Hyperlink) |
| 164 | +* 2.3.61 CT_Hyperlink |
0 commit comments