Class LoContext

The LoContext class is a context manager that provides a way to use multiple documents in OooDev.

Classes in ooodev.Draw, ooodev.Calc, ooodev.Writer namespaces already use this context manager to enable the use of multiple documents.

In some cases you may want to use muli-documents in your own code. In this case you can use the LoContext class directly.

Only static classes such as Class Lo, Class Info, Class GUI, and classes in the office namespace would require the use of the LoContext class in multi-document mode. Methods and properties of these classes marked as |lo_safe| or |lo_unsafe|. This makes them easy to identify. If marked as |lo_safe| they can be used in multi-document mode without the need to use the LoContext class.

An example of use is shown below:

from import Info
from ooodev.write import WriteDoc
from ooodev.utils.context import LoContext

# create first document
doc1  = WriteDoc.create_doc()

# create a second document
lo_inst = Lo.create_lo_instance()
doc2  = WriteDoc.create_doc(lo_inst=lo_inst)

# get fonts of first document
# the default context, which is for the first document is used.
fonts =  Info.get_fonts()

# get fonts of second document
With LoContext(lo_inst=doc2.lo_inst):
    # The context is changed to the second document.
    # As soon as the LoContext block is exited, the context is
    # restored to the first document.
    fonts =  Info.get_fonts()

Class Declaration

class ooodev.utils.context.LoContext(inst, timeout_ms=30000)[source]

LoContext is a context manager for switching the current LibreOffice context to a different instance.

This allows for multiple instances of LibreOffice Documents to be used at the same time.

  • inst (LoInst) –

  • timeout_ms (float) –

__init__(inst, timeout_ms=30000)[source]

Constructor for LoContext

  • inst (LoInst) – The instance of LO Context to switch to.

  • timeout_ms (float, optional) – Timeout in milliseconds. Set to 0 for no timeout. Defaults to 30_000 ( 30 seconds).

Return type: