MCPcopy Index your code
hub / github.com/python-openxml/python-docx / Hyperlink

Class Hyperlink

src/docx/text/hyperlink.py:20–121  ·  view source on GitHub ↗

Proxy object wrapping a ` ` element. A hyperlink occurs as a child of a paragraph, at the same level as a Run. A hyperlink itself contains runs, which is where the visible text of the hyperlink is stored.

Source from the content-addressed store, hash-verified

18
19
20class Hyperlink(Parented):
21 """Proxy object wrapping a `<w:hyperlink>` element.
22
23 A hyperlink occurs as a child of a paragraph, at the same level as a Run. A
24 hyperlink itself contains runs, which is where the visible text of the hyperlink is
25 stored.
26 """
27
28 def __init__(self, hyperlink: CT_Hyperlink, parent: t.ProvidesStoryPart):
29 super().__init__(parent)
30 self._parent = parent
31 self._hyperlink = self._element = hyperlink
32
33 @property
34 def address(self) -> str:
35 """The "URL" of the hyperlink (but not necessarily a web link).
36
37 While commonly a web link like "https://google.com" the hyperlink address can
38 take a variety of forms including "internal links" to bookmarked locations
39 within the document. When this hyperlink is an internal "jump" to for example a
40 heading from the table-of-contents (TOC), the address is blank. The bookmark
41 reference (like "_Toc147925734") is stored in the `.fragment` property.
42 """
43 rId = self._hyperlink.rId
44 return self._parent.part.rels[rId].target_ref if rId else ""
45
46 @property
47 def contains_page_break(self) -> bool:
48 """True when the text of this hyperlink is broken across page boundaries.
49
50 This is not uncommon and can happen for example when the hyperlink text is
51 multiple words and occurs in the last line of a page. Theoretically, a hyperlink
52 can contain more than one page break but that would be extremely uncommon in
53 practice. Still, this value should be understood to mean that "one-or-more"
54 rendered page breaks are present.
55 """
56 return bool(self._hyperlink.lastRenderedPageBreaks)
57
58 @property
59 def fragment(self) -> str:
60 """Reference like `#glossary` at end of URL that refers to a sub-resource.
61
62 Note that this value does not include the fragment-separator character ("#").
63
64 This value is known as a "named anchor" in an HTML context and "anchor" in the
65 MS API, but an "anchor" element (`<a>`) represents a full hyperlink in HTML so
66 we avoid confusion by using the more precise RFC 3986 naming "URI fragment".
67
68 These are also used to refer to bookmarks within the same document, in which
69 case the `.address` value with be blank ("") and this property will hold a
70 value like "_Toc147925734".
71
72 To reliably get an entire web URL you will need to concatenate this with the
73 `.address` value, separated by "#" when both are present. Consider using the
74 `.url` property for that purpose.
75
76 Word sometimes stores a fragment in this property (an XML attribute) and
77 sometimes with the address, depending on how the URL is inserted, so don&#x27;t

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…