When importing a module with Python that operates LibreOffice, perform the following module search in addition to normal. --The \ <LibreOffice installation directory > / program directory is automatically added to the module search path sys.path when the macro is executed. --When the macro is executed, the pythonpath subdirectory of the macro installation directory and the pythonpath.zip file, if any, are automatically added to sys.path. --From a macro or a program that imports \ <LibreOffice installation directory > /program/uno.py, you can import from UNO-specific modules starting with com.sun.star. You cannot import the module itself.
When running Python macros, the \ <LibreOffice installation directory > / program directory is included in sys.path. \ <LibreOffice installation directory > is / usr / local / lib / libreoffice for FreeBSD and% PROGRAMFILES% \ LibreOffice for Microsoft Windows. Import uno, unohelper, pythonscript, msgbox from here.
If the pythonpath subdirectory of the directory where the macro is installed or the pythonpath.zip file exists, it will be added to sys.path to make the module search symmetric.
For user macros,
--For Microsoft Windows
%APPDATA%\LibreOffice\4\user\Scripts\python\pythonpath
%APPDATA%\LibreOffice\4\user\Scripts\python\pythonpath.zip
--For FreeBSD or UNIX-like systems
${HOME}/.config/libreoffice/4/user/Scripts/python/pythonpath
${HOME}/.config/libreoffice/4/user/Scripts/python/pythonpath.zip
Will be. These paths are maintained after the macro exits until you exit LibreOffice. It is presumed that it holds the Loader class in \ <LibreOffice installation directory > /program/pythonloader.py. The checkForPythonPathBesideComponent () function in the same file is used to check for existence and add to sys.path.
System shared macros and extensions are also added, so there is a possibility that sys.path contains unnecessary directories when calling macros.
For example, try running the following macro to display the contents of sys.path in a message box.
syspath.py
import sys
from msgbox import MsgBox
def syspath_msgbox():
ctx = XSCRIPTCONTEXT.getComponentContext()
sys_path_box = MsgBox(ctx)
sys_path_box.addButton('oK')
sys_path_box.renderFromBoxSize(300)
sys_path = '\n'.join(sys.path)
sys_path_box.show(sys_path, 0, 'sys.path')
The following is displayed. At this time, the pythonpath subdirectory is created, but the pythonpath.zip file does not exist. The second displayed / home / shota243 is the current directory when LibreOffice is started. At this time, the PYTHONPATH environment variable is not set.
/usr/local/lib/libreoffice/program
/home/shota243
/usr/local/lib/python37.zip
/usr/local/lib/python3.7
/usr/local/lib/python3.7/lib-dynload
/home/shota243/.local/lib/python3.7/site-packages
/usr/local/lib/python3.7/site-packages
/home/shota/.config/libreoffice/4/user/Scripts/python/pythonpath
APSO --Alternative Script Organizer for Python View sys.path from the extension Python interpreter console If you do, it is as follows.
APSO python console [LibreOffice]
3.7.7 (default, Jun 21 2020, 22:27:00)
[Clang 8.0.0 (tags/RELEASE_800/final 356365)]
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> for p in sys.path:
... print(p)
...
/usr/local/lib/libreoffice/program
/home/shota243
/usr/local/lib/python37.zip
/usr/local/lib/python3.7
/usr/local/lib/python3.7/lib-dynload
/home/shota243/.local/lib/python3.7/site-packages
/usr/local/lib/python3.7/site-packages
/home/shota243/.config/libreoffice/4/user/uno_packages/cache/uno_packages/lu2030qffpne.tmp_/apso.oxt/python/pythonpath
>>>
The installation destination of the APSO extension is /home/shota243/.config/libreoffice/4/user/uno_packages/cache/uno_packages/lu2030qffpne.tmp_. (Omitted) lu2030qffpne.tmp_ / apso.oxt / python / apso.py is the main body (Omitted) under the lu2030qffpne.tmp_ / apso.oxt / python / pythonpath directory The modules are apso_debug.py, apso_utils.py, and theconsole.py.
Programs that import macros or uno can import identifiers from modules that start with com.sun.star.
This is because the \ _ \ _ import \ _ \ _ () function, which is the actual state of import, has been replaced with the \ _uno \ _import () function in the \ <LibreOffice installation directory > /program/uno.py. com does not exist anywhere in the module search path. In the \ _uno \ _import () function, if the module search with the standard \ _ \ _ import \ _ \ _ () function fails, the pyuno.getClass () function tries to get it. At this time, \ <module name > and \ <identifier > of from \ <module name > import \ <identifier > are concatenated to call getClass (), so the entire \ <module name > can be imported or from. Bulk import with \ <module name > import * is an exception.
Example:
mike% export PYTHONPATH="${PYTHONPATH}:/usr/local/lib/libreoffice/program"
mike% export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/usr/local/lib/libreoffice/program"
mike% export PATH="${PATH}:/usr/local/lib/libreoffice/program"
mike% python
python
Python 3.7.7 (default, Jun 21 2020, 22:27:00)
[Clang 8.0.0 (tags/RELEASE_800/final 356365)] on freebsd11
Type "help", "copyright", "credits" or "license" for more information.
>>> import uno
import uno
>>> from com.sun.star.uno import RuntimeException
from com.sun.star.uno import RuntimeException
>>> import com.sun.start.uno.RuntimeException
import com.sun.start.uno.RuntimeException
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/libreoffice/program/uno.py", line 356, in _uno_import
return _builtin_import(name, *optargs, **kwargs)
ModuleNotFoundError: No module named 'com'
>>> from com.sun.star.uno import *
from com.sun.star.uno import *
from com.sun.star.uno import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/libreoffice/program/uno.py", line 434, in _uno_import
raise uno_import_exc
File "/usr/local/lib/libreoffice/program/uno.py", line 356, in _uno_import
return _builtin_import(name, *optargs, **kwargs)
ImportError: No module named 'com' (or 'com.sun.star.uno.*' is unknown)
>>>
The reference of modules and identifiers that can be imported is considered to be com :: sun :: star Module Reference.
Recommended Posts