Hyperlink ========= Word allows a hyperlink to be placed in a document wherever a paragraph can appear. The actual hyperlink element is a peer of |Run|. The link may be to an external resource such as a web site, or internal, to another location in the document. The link may also be a `mailto:` URI or a reference to a file on an accessible local or network filesystem. The visible text of a hyperlink is held in one or more runs. Technically a hyperlink can have zero runs, but this occurs only in contrived cases (otherwise there would be nothing to click on). As usual, each run can have its own distinct text formatting (font), so for example one word in the hyperlink can be bold, etc. By default, Word applies the built-in `Hyperlink` character style to a newly inserted hyperlink. Like other text, the hyperlink text may often be broken into multiple runs as a result of edits in different "revision-save" editing sessions (between "Save" commands). Note that rendered page-breaks can occur in the middle of a hyperlink. A |Hyperlink| is a child of |Paragraph|, a peer of |Run|. TODO: What about URL-encoding/decoding (like %20) behaviors, if any? Candidate protocol ------------------ An external hyperlink has an address and an optional anchor. An internal hyperlink has only an anchor. An anchor is more precisely known as a *URI fragment* in a web URL and follows a hash mark ("#"). The fragment-separator hash character is not stored in the XML. Note that the anchor and address are stored in two distinct attributes, so you need to concatenate `.address` and `.anchor` like `f"{address}#{anchor}"` if you want the whole thing. Also note that Word does not rigorously separate a fragment in a web URI so it may appear as part of the address or separately in the anchor attribute, depending on how the hyperlink was authored. Hyperlinks inserted using the dialog-box seem to separate it and addresses typed into the document directly don't, based on my limited experience. .. highlight:: python **Access hyperlinks in a paragraph**:: >>> hyperlinks = paragraph.hyperlinks [] **Access hyperlinks in a paragraph in document order with runs**:: >>> list(paragraph.iter_inner_content()) [ ] **Access hyperlink address**:: >>> hyperlink.address 'https://google.com/' **Access hyperlink fragment**:: >>> hyperlink.fragment 'introduction' **Access hyperlink history (visited or not, True means not visited yet)**:: >>> hyperlink.history True **Access hyperlinks runs**:: >>> hyperlink.runs [ ] **Access hyperlink URL**:: >>> hyperlink.url 'https://us.com#introduction' **Determine whether a hyperlink contains a rendered page-break**:: >>> hyperlink.contains_page_break False **Access visible text of a hyperlink**:: >>> hyperlink.text 'an excellent Wikipedia article on ferrets' **Add an external hyperlink** (not yet implemented):: >>> hyperlink = paragraph.add_hyperlink( ... 'About', address='http://us.com', fragment='about' ... ) >>> hyperlink >>> hyperlink.text 'About' >>> hyperlink.address 'http://us.com' >>> hyperlink.fragment 'about' >>> hyperlink.url 'http://us.com#about' **Add an internal hyperlink (to a bookmark)**:: >>> hyperlink = paragraph.add_hyperlink('Section 1', fragment='Section_1') >>> hyperlink.text 'Section 1' >>> hyperlink.fragment 'Section_1' >>> hyperlink.address '' **Modify hyperlink properties**:: >>> hyperlink.text = 'Froogle' >>> hyperlink.text 'Froogle' >>> hyperlink.address = 'mailto:info@froogle.com?subject=sup dawg?' >>> hyperlink.address 'mailto:info@froogle.com?subject=sup%20dawg%3F' >>> hyperlink.anchor = None >>> hyperlink.anchor None **Add additional runs to a hyperlink**:: >>> hyperlink.text = 'A ' >>> # .insert_run inserts a new run at idx, defaults to idx=-1 >>> hyperlink.insert_run(' link').bold = True >>> hyperlink.insert_run('formatted', idx=1).bold = True >>> hyperlink.text 'A formatted link' >>> [r for r in hyperlink.iter_runs()] [, , ] **Iterate over the run-level items a paragraph contains**:: >>> paragraph = document.add_paragraph('A paragraph having a link to: ') >>> paragraph.add_hyperlink(text='github', address='http://github.com') >>> [item for item in paragraph.iter_run_level_items()]: [, ] **Paragraph.text now includes text contained in a hyperlink**:: >>> paragraph.text 'A paragraph having a link to: github' Word Behaviors -------------- * What are the semantics of the w:history attribute on w:hyperlink? I'm suspecting this indicates whether the link should show up blue (unvisited) or purple (visited). I'm inclined to think we need that as a read/write property on hyperlink. We should see what the MS API does on this count. * We probably need to enforce some character-set restrictions on w:anchor. Word doesn't seem to like spaces or hyphens, for example. The simple type ST_String doesn't look like it takes care of this. * We'll need to test URL escaping of special characters like spaces and question marks in Hyperlink.address. * What does Word do when loading a document containing an internal hyperlink having an anchor value that doesn't match an existing bookmark? We'll want to know because we're sure to get support inquiries from folks who don't match those up and wonder why they get a repair error or whatever. Specimen XML ------------ .. highlight:: xml External links ~~~~~~~~~~~~~~ The address (URL) of an external hyperlink is stored in the document.xml.rels file, keyed by the w:hyperlink@r:id attribute:: This is an external link to Google ... mapping to relationship in document.xml.rels:: A hyperlink can contain multiple runs of text (and a whole lot of other stuff, at least as far as the schema indicates):: A hyperlink containing an italicized word Internal links ~~~~~~~~~~~~~~ An internal link provides "jump to another document location" behavior in the Word UI. An internal link is distinguished by the absence of an r:id attribute. In this case, the w:anchor attribute is required. The value of the anchor attribute is the name of a bookmark in the document. Example:: See Section 4 for more details. ... referring to this bookmark elsewhere in the document:: Section 4 Schema excerpt -------------- .. highlight:: xml ::