Write Direct Character Hyperlink Class
The ooodev.format.writer.direct.char.hyperlink.Hyperlink
class is used to create a hyperlink in a document.
Setting the style
General function used to run these examples.
import sys
from ooodev.format.writer.direct.char.hyperlink import Hyperlink, TargetKind
from ooodev.office.write import Write
from ooodev.gui import GUI
from ooodev.loader.lo import Lo
def main() -> int:
with Lo.Loader(Lo.ConnectPipe()):
doc = Write.create_doc()
GUI.set_visible(doc=doc)
Lo.delay(300)
GUI.zoom(GUI.ZoomEnum.ENTIRE_PAGE)
cursor = Write.get_cursor(doc)
ln_name = "OooDev"
ln_url = "https://python-ooo-dev-tools.readthedocs.io/en/latest/"
hl = Hyperlink(name=ln_name, url=ln_url)
Write.append(cursor, "OOO Development Tools", (hl,))
Write.append_para(cursor, " Docs")
Write.append(cursor, "Source on Github ")
pos = Write.get_position(cursor)
ln_name = "OOODEV_GITHUB"
ln_url = "https://github.com/Amourspirit/python_ooo_dev_tools"
hl = Hyperlink(name=ln_name, url=ln_url, target=TargetKind.BLANK)
Write.append(cursor, "OOO Development Tools on Github", (hl,))
Lo.delay(2_000)
# remove the hyperlink
Write.style_left(cursor=cursor, pos=pos, styles=(hl.empty,))
# get the hyperlink from the document
cursor.gotoStart(False)
cursor.goRight(21, True)
ooo_dev_hl = Hyperlink.from_obj(cursor)
cursor.gotoEnd(False)
print(ooo_dev_hl.prop_url)
Lo.delay(1_000)
Lo.close_doc(doc)
return 0
if __name__ == "__main__":
sys.exit(main())
Examples
Adding Hyperlink
cursor = Write.get_cursor(doc)
ln_name = "OooDev"
ln_url = "https://python-ooo-dev-tools.readthedocs.io/en/latest/"
hl = Hyperlink(name=ln_name, url=ln_url)
Write.append(cursor, "OOO Development Tools", (hl,))
Write.append_para(cursor, " Docs")
# ... other code
Remove Hyperlink
cursor = Write.get_cursor(doc)
# ... other code
pos = Write.get_position(cursor)
# ... other code
Write.style_left(cursor=cursor, pos=pos, styles=(hl.empty,))
Getting the Hyperlink from the text
# ... other code
# get the hyperlink from the document
cursor.gotoStart(False)
cursor.goRight(21, True)
ooo_dev_hl = Hyperlink.from_obj(cursor)
cursor.gotoEnd(False)
print(ooo_dev_hl.prop_url)