MCPcopy Create free account
hub / github.com/scanny/python-pptx / Hyperlink

Class Hyperlink

src/pptx/action.py:193–270  ·  view source on GitHub ↗

Represents a hyperlink action on a shape or text run.

Source from the content-addressed store, hash-verified

191
192
193class Hyperlink(Subshape):
194 """Represents a hyperlink action on a shape or text run."""
195
196 def __init__(
197 self,
198 xPr: CT_NonVisualDrawingProps | CT_TextCharacterProperties,
199 parent: BaseShape,
200 hover: bool = False,
201 ):
202 super(Hyperlink, self).__init__(parent)
203 # xPr is either a cNvPr or rPr element
204 self._element = xPr
205 # _hover determines use of `a:hlinkClick` or `a:hlinkHover`
206 self._hover = hover
207
208 @property
209 def address(self) -> str | None:
210 """Read/write. The URL of the hyperlink.
211
212 URL can be on http, https, mailto, or file scheme; others may work. Returns |None| if no
213 hyperlink is defined, including when another action such as `RUN_MACRO` is defined on the
214 object. Assigning |None| removes any action defined on the object, whether it is a hyperlink
215 action or not.
216 """
217 hlink = self._hlink
218
219 # there's no URL if there's no click action
220 if hlink is None:
221 return None
222
223 # a click action without a relationship has no URL
224 rId = hlink.rId
225 if not rId:
226 return None
227
228 return self.part.target_ref(rId)
229
230 @address.setter
231 def address(self, url: str | None):
232 # implements all three of add, change, and remove hyperlink
233 self._remove_hlink()
234
235 if url:
236 rId = self.part.relate_to(url, RT.HYPERLINK, is_external=True)
237 hlink = self._get_or_add_hlink()
238 hlink.rId = rId
239
240 def _get_or_add_hlink(self) -> CT_Hyperlink:
241 """Get the `a:hlinkClick` or `a:hlinkHover` element for the Hyperlink object.
242
243 The actual element depends on the value of `self._hover`. Create the element if not present.
244 """
245 if self._hover:
246 return cast("CT_NonVisualDrawingProps", self._element).get_or_add_hlinkHover()
247 return self._element.get_or_add_hlinkClick()
248
249 @property
250 def _hlink(self) -> CT_Hyperlink | None:

Callers 5

address_fixtureMethod · 0.90
no_address_fixtureMethod · 0.90
remove_fixtureMethod · 0.90
update_fixtureMethod · 0.90
hyperlinkMethod · 0.85

Calls

no outgoing calls

Tested by 4

address_fixtureMethod · 0.72
no_address_fixtureMethod · 0.72
remove_fixtureMethod · 0.72
update_fixtureMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…