Resolving LibreOffice python imports in VS Code (com.sun.star...)

I’m working with setting up VS Code like an editor for Python scripts run by LibreOffice.

The Pylance component in VS Code stumbles on all imports on this pattern:

from com.sun.star.text.TextContentAnchorType import AS_CHARACTER
from com.sun.star.awt import Size

And also on the variable XSCRIPTCONTEXT.

I know these references are generated, as I cannot seem to find any Python files that declare them. Where/how are they declared ?

I would like to add some path or file pointer to VsCode - so that PyLance could resolve these imports.

Advice ?

Hints for the use of IDEs (but not VSCode) are in the help-files:
https://wiki.documentfoundation.org/Macros/Python_Basics

Maybe the hints mentioned here:
https://wiki.documentfoundation.org/Macros/Python_Design_Guide
are also useful, especially the reference to the french “XSCRIPTCONTEXT revisete”

The Python runtime needs to be compiled by the same compiler as the office suite. The runtime can be found at <Install_dir>/program/python[.exe].
With a Linux distribution everything the binaries fall out of the same compiler, so you can use the distribution’s native Python and office suite.

Currently I have both LibreOffice and Vs Code running on Windows 10.

The Python interpreter is there as: c/Program Files/LibreOffice/program/python-core-3.8.10/bin/python.exe - installed by LibreOffice setup.

Inside of Vs Code I have swapped to this python interpreter.

On my Linux system XSCRIPTCONTEXT is defined in pythonscript.py
The entire object hierarchy comes from a framework called “Unified Network Objects” (UNO).

Thanks, I located the pythonscript.py file. I would like PyLance (or the language server) to by default read this file. But I can only add directories to that one (where it finds the imported modules).

I guess the UNO bindings are generated by C/C++, and somehow exposed to the Python interpreter. And that there are no corresponding Python source files (?) .

All languages use the same Unified Network Object. All it takes is a little bit of glue code.

Yes, and somehow Python can then use this code:

from com.sun.star.util import XModifyListener

But there is nowhere in the file system a file com/sun/star/util.py (as would be for most other import statements).

How can an external tool (like a language server) get hold of the same information as the Python interpreter ?

Maybe I need to search this piece from other sources…

https://api.libreoffice.org/docs/idl/ref/namespacecom_1_1sun_1_1star_1_1util.html
[Tutorial] Introduction into object inspection with MRI

Both links are interesting and bring me new knowledge. The MRI is an interesting tool (I had used it but the articles points out new useful things to me).

You could say my original question was more how to make the code analyzer in VS Code go quiet - by resolving the import paths. I do use the output of the code analyzer - as it picks up bugs. It gets a bit overloaded now by these unnecessary references to UNO function calls it cannot resolve.

I’ll live with it for now.

I attach an image illustrating the warnings I wanted to resolve.

So your question is more on configuration of pylance. I could find how to silence missing imports via google (pylance ignore missing imports) at Disable specific Pylance linting messages in VS Code settings.json like with "python.linting.pylintArgs" - Stack Overflow

Thanks. I really would like it to be able to resolve those calls. But if there’s no way, then it is so. Your suggestion removes the symptoms (and disables this whole category of warnings from the code analyzer) - better than no solution at all.

Time passes by…
.
One could check the project at the following link. I don’t use it, but it seem to integrate some glue-code to the LibreOffice-API to python.

XSCRIPTCONTEXT global variable is only available within LO on open documents.

Debugging remote Python from IDES such as PyCharm or VS requires the setup of LO as a Service. In which case one needs to create an XSCRIPTCONTEXT adapter - see Python Basics - or to substitute XSCRIPTCONTEXT features importing UNO.
The latter is described in programming with Python scripts

Nonsense… from a »remote« interactive python-session via Jupyter-notebook:

import uno    
from pythonscript import ScriptContext

PIPENAME = "your_pipename_here"  # todo: start Lo in pipe-mode

args = f"pipe,name={PIPENAME}"
local = uno.getComponentContext()
resolver = local.ServiceManager.createInstance("com.sun.star.bridge.UnoUrlResolver")
ctx = resolver.resolve( f"uno:{args};"
                           "urp;"
                           "StarOffice.ComponentContext")

createUnoService = ctx.ServiceManager.createInstance

XSCRIPTCONTEXT = ScriptContext( ctx , None, None )
  • after executing the code above there is »XSCRIPTCONTEXT« available!
  • also the method »createUnoService« which is mostly the very same as its BASIC-counterpart
  • also code-completition|suggestions after typing: some_object.<tab>

ps. and YES, both PyCharm and VSCode provide Plugins for working directly with jupyter-notebook-files

Thanks Karolus

In your example XSCRIPTCONTEXT is instantiated - therefore still absent as a global variable - meaning it’s more or less substituted as suggested.

My prayer: Can you provide instructions as to how to setup an “interactive Python-session via Jupyter-notebook”?

in the context linux with LO from your distro linked to the distro-python-interpreter?:
in a terminal:

## DONT use pip with sudo!!!
pip install notebook  --break-system-packages --upgrade
#create a dedicated folder for LO-related notebooks:
mkdir ~/.config/libreoffice/4/user/ipython
cd ~/.config/libreoffice/4/user/ipython
jupyter-notebook

The last command triggers a jupyter-dashboard in your webbrowser[1], from there you may continue by your own

[1] It runs completly local without calls to somewhere in the Internet

[2] in some distros you may need first:

sudo apt-get install libreoffice-script-provider-python
sudo apt-get install python3-zmq


in the context linux with Libreoffice via flatpak:

1. create python-venv for LO-Flatpak:

1.1 change to LO-Runtime, initialize the venv and install »notebook«:
… $ flatpak run --command=bash org.libreoffice.LibreOffice
… $ python -m venv /var/data/python
… $ cd /var/data/python
… $ source bin/activate
(python) … $ pip install pip --upgrade
(python) … $ pip install notebook --upgrade
2. … create a shellscript with content:
#!/usr/bin/bash

# URE_BOOTSTRAP……fundamentalrc is mandatory for uno-imports
# extra-PATH to »jupyter-notebook«
# extra-PYTHONPATH to the libreoffice/program install-directory

/usr/bin/flatpak run \
--env=URE_BOOTSTRAP=vnd.sun.star.pathname:/app/libreoffice/program/fundamentalrc \
--env=PATH=$PATH:/var/data/python/bin \
--env=PYTHONPATH=$PYTHONPATH:/app/libreoffice/program \
--command=/var/data/python/bin/jupyter-notebook  \
--file-forwarding  \
org.libreoffice.LibreOffice

make the script executeable
create a .desktop file with command /path/to/your/shellscript.sh
and a working-directory to your …flatpak-user-config/ipython

btw. @sberg : could you place such instructions into flatpak-Libreoffice-ReadMe ?

1 Like

:+1:
Happy :snake: !