Class LoEvents

LoEvents is a singleton class.

Because this is a singleton class it will continue to fire events as long as your running code has one reference to LoEvents.

Think of LoEvents as being global scoped.

Events can be removed via the remove() method. It is not possible to remove OooDev built in events.

Perhaps a better solution to subscribe to event is the Events class as it has a local scope.

Warning

Subscribing to events on this class can have unexpected side effects. Such as subscribed events being triggered when you thought you code was finished running.

In most case the it is recommended to use Class Events to hook events.

Each time OooDev internally runs a print command a event is raised with CancelEventArgs as the args parameter. If that event is hooked then the print can be canceled.

In the following example all OooDev internal print commands are canceled.

Example
#!/usr/bin/env python
# coding: utf-8
from __future__ import annotations
import argparse
from typing import Any, cast

from ooodev.loader.lo import Lo
from ooodev.office.write import Write
from ooodev.utils.info import Info
from ooodev.wrapper.break_context import BreakContext
from ooodev.events.gbl_named_event import GblNamedEvent
from ooodev.events.args.cancel_event_args import CancelEventArgs
from ooodev.events.lo_events import LoEvents


def args_add(parser: argparse.ArgumentParser) -> None:
    parser.add_argument(
        "-f",
        "--file",
        help="File path of input file to convert",
        action="store",
        dest="file_path",
        required=True,
    )

def on_lo_print(source: Any, e: CancelEventArgs) -> None:
    e.cancel = True

def main() -> int:
    parser = argparse.ArgumentParser(description="main")
    args_add(parser=parser)
    args = parser.parse_args()

    # hook ooodev internal printing event
    LoEvents().on(GblNamedEvent.PRINTING, on_lo_print)

    with BreakContext(Lo.Loader(Lo.ConnectSocket(headless=True))) as loader:

        fnm = cast(str, args.file_path)

        try:
            doc = Lo.open_doc(fnm=fnm, loader=loader)
        except Exception:
            print(f"Could not open '{fnm}'")
            raise BreakContext.Break

        if Info.is_doc_type(obj=doc, doc_type=Lo.Service.WRITER):
            text_doc = Write.get_text_doc(doc=doc)
            cursor = Write.get_cursor(text_doc)
            text = Write.get_all_text(cursor)
            print("Text Content".center(50, "-"))
            print(text)
            print("-" * 50)
        else:
            print("Extraction unsupported for this doc type")
        Lo.close_doc(doc)

    return 0


if __name__ == "__main__":
    raise SystemExit(main())

See also

Class Events

Note

Events are limited to OooDev.

For instance OooDev can capture when are row is being removed from a spreadsheet internally; However, OooDev is not aware when other methods are used such as user input.

For more indepth Events look into LibreOffice Listeners and Chapter 4. Listening, and Other Techniques.

class ooodev.events.lo_events.LoEvents[source]

Singleton Class for ODEV global events.

__init__()[source]
Return type:

None

add_observer(*args)[source]

Adds observers that gets their trigger method called when this class trigger method is called.

Parameters:

args (EventObserver) – One or more observers to add.

Return type:

None

Note

Observers are removed automatically when they are out of scope.

has_event(event_name, callback)

Gets if event exists.

Parameters:
  • event_name (str) – Event name

  • callback (EventCallback) – Callback function

Returns:

True if event exists; Otherwise, False

Return type:

bool

has_event_name(event_name)

Gets if event exists.

Parameters:

event_name (str) – Event name

Returns:

True if event exists; Otherwise, False

Return type:

bool

on(event_name, callback)

Registers an event

Parameters:
  • event_name (str) – Unique event name

  • callback (Callable[[object, EventArgs], None]) – Callback function

remove(event_name, callback)

Removes an event callback

Parameters:
  • event_name (str) – Unique event name

  • callback (Callable[[object, EventArgs], None]) – Callback function

Returns:

True if callback has been removed; Otherwise, False.

False means the callback was not found.

Return type:

bool

remove_observer(observer)

Removes an observer.

Parameters:

observer (EventObserver) – Observers to remove.

Returns:

True if observer has been removed; Otherwise, False.

Return type:

bool

trigger(event_name, event_args)[source]

Trigger event(s) for a given name.

Parameters:
  • event_name (str) – Name of event to trigger

  • event_args (EventArgsT) – Event args passed to the callback for trigger.

  • args (Any, optional) – Optional positional args to pass to callback

  • kwargs (Any, optional) – Optional keyword args to pass to callback

Note

Events are removed automatically when they are out of scope.