Working with LibreOffice in Python: import

Version used

Overview

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.

Macro sys.path

\ <LibreOffice installation directory > / program directory

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.

pythonpath directory and pythonpath.zip file

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.

Import from UNO specific module

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

Working with LibreOffice in Python: import
Working with LibreOffice in Python
Working with sounds in Python
Working with DICOM images in Python
Try working with binary data in Python
Post Test 3 (Working with PosgreSQL in Python)
Import tsv with Python
Try working with Mongo in Python on Mac
[Introduction for beginners] Working with MySQL in Python
Scraping with selenium in Python
Scraping with chromedriver in python
Dynamically import scripts in Python
Import vtk with brew python
Debugging with pdb in Python
Python: Working with Firefox with selenium
Scraping with Selenium in Python
Scraping with Tor in Python
Tweet with image in Python
Combined with permutations in Python
Specific sample code for working with SQLite3 in Python
Run a Python file with relative import in PyCharm
Number recognition in images with Python
Testing with random numbers in Python
GOTO in Python with Sublime Text 3
Scraping with Selenium in Python (Basic)
CSS parsing with cssutils in Python
Numer0n with items made in Python
Open UTF-8 with BOM in Python
Use rospy with virtualenv in Python3
Use Python in pyenv with NeoVim
Heatmap with Dendrogram in Python + matplotlib
Read files in parallel with Python
Password generation in texto with python
Use OpenCV with Python 3 in Window
Until dealing with python in Atom
Get started with Python in Blender
Write documentation in Sphinx with Python Livereload
Get additional data in LDAP with python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Try logging in to qiita with Python
Stress Test with Locust written in Python
Python3> in keyword> True with partial match?
Exclusive control with lock file in Python
Device monitoring with On-box Python in IOS-XE
Draw Nozomi Sasaki in Excel with python
Tips for dealing with binaries in Python
Display Python 3 in the browser with MAMP
Page cache in Python + Flask with Flask-Caching
How to work with BigQuery in Python
Playing card class in Python (with comparison)
Working with OpenStack using the Python SDK
Working with Azure CosmosDB from Python Part.2
Dealing with "years and months" in Python
Process multiple lists with for in Python
Replace non-ASCII with regular expressions in Python
Connect with mysql.connector with ssh tunnel in Python 3.7
One liner webServer (with CGI) in python
Get Started with TopCoder in Python (2020 Edition)
Easy image processing in Python with Pillow
To work with timestamp stations in Python
Call APIGateWay with APIKey in python requests