What is XSCRIPTCONTEXT ?

When you program a script within LibreOffice, accessing the XSCRIPTCONTEXT object is the starting point for controlling LibreOffice documents.

SCRIPTCONTEXT is an object of class ‘pythonscript.ScriptContext’.
This object provides connections with running instances of LibreOffice and has four useful methods:
getDocument ()
getInvocationContext ()
getDesktop ()
getComponentContext ()

Still hungry?

Here pythonscript is the python program that defines the context for Python scripts in . You can find pythonscript.py in /lib/libreoffice/program on Linux, probably in a similar path on MacOs and in C:\Program Files\LibreOffice\program on Windows (standard install).
ScriptContext is a Class inherited from unohelper.base and defined in pythonscript.py.
On linux Mint, unohelper.py is in /lib/python3/dist-packages and MacOS (most likely), and in C:\Program Files\LibreOffice\program on Windows (standard install).
The unohelper.Base itself is a class inherited from XTypeProvider, the latest being imported from com.sun.star.lang and being inherited from XInterface.

2 Comments

  1. Charles Gregoire

    Wondering if possible to use additional python modules within a python macro such as sqlite3.

    When I tried to load the module within a libreoffice python file it couldn’t find the module. Is there a way to use the rich set of modules available when using python outside the libreoffice calc domain?

    Reply
    1. gweno (Post author)

      Hi Charles. Thanks for your question. Importing other modules like sqlite3 into your LibreOffice python scripts shouldn’t be a problem. Have you checked that the module sqlite3 is in a ‘sys.path’ folder?
      It should be part of the Python bundle already, to make sure it’s there open in a python shell, like the APSO shell within LibreOffice, Idle or any other Python Shell, by first importing module sys, then executing sys.path:
      import sys
      sys.path

      It will return a list containing all the folders that python checks when looking for a module.
      Then you need to check the path for sqlite3:
      import os
      import sqlite3
      print(os.path.dirname(sqlite3.__file__))

      Of course if you already get an ModuleNotFoundError error this is a good sign that sqlite3 is not installed in a path known by python.
      In which case you’ll need to locate where sqlite3 is and insert it to sys.path in your script:
      import sys
      sys.path.insert(0,'/path/to/mod_directory')

      Please share any error message if it still doesn’t work. Good luck

      Reply

Leave a Comment

Your email address will not be published. Required fields are marked *