In my previous blog post on creating LibreOffice extensions with Python, I have discussed how to write a Python code that works with LibreOffice API, and can be run and debugged in an IDE, and packed later in an extension. Now I discuss how to debug the Python code.
Here you can read the previous post:
Preparing the Debug Environment
First, you need the minimal extension, and the small Python script inside it. The complete extension as described in the previous post, is available below, and the plan is to make it available among the other LibreOffice SDK examples:
For debugging, you can open the Python file
main.py, and start editing it, adding the required functionalities.
To debug the Python program, the best way is to use an IDE with the LibreOffice internal Python as the interpreter. You only need to install LibreOffice, and LibreOffice SDK from libreoffice.org/download.
The LibreOffice internal Python interpreter resides in the program folder of the LibreOffice installation. The Python package for accessing UNO components is already installed.
In Linux, the default location of the interpreter is (for LibreOffice 7.6):
In macOS, the default location is:
And on Windows, the default location is:
Configuring the Interpreter in an IDE
This is an example python configuration for PyCharm, a popular Python IDE.
Setting LibreOffice internal Python in PyCharm
With this configuration, you should be fine to start the debugging. By just clicking on the debug button, you can start running the script line by line, and use the debugging utilities in the IDE to check the value of the variables in order to be diagnose the possible problems.
As described in the previous part of the blog post, in the example Python code, the context is handled in different situations. Thus, you can use the same Python script and pack it as an extension.
Unfortunately, the autocomplete does not currently work for the methods that are available in the UNO components. This is because they are bridged dynamically, and thus autocomplete can not create the list of usable methods.
This can be improved with the use of Python auto-completion feature called REPL, and a utility package. Here I suggest using ptpython:
First, you need to install pip in LibreOffice internal python using PIP bootstrap, or ZazPip package. To do that, please refer to this question and answer on Ask LibreOffice:
Then you should be able to use Python interactive console with auto-completion. To use it, you have to run this small code:
from ptpython.repl import embed
This method works both on Windows and Linux, but not on APSO console. It is important to mention that this is not ideal as it only works in interactive console, and also does not provide the details about the function parameters.
For further information, please refer to the TDF Wiki page:
There you will find configuration for various IDEs to set up environment for debugging Python scripts that work with LibreOffice API.