Search code examples
pythonlibreofficeuno

Installing pyuno (LibreOffice) for private Python build


There are a few related threads about this topic here ad here but they seem a bit dated.

I just downloaded LibreOffice 4 which has a Python 3.3.0 built in. Using that Python I can import and use UNO just fine, and control Office from my Python script. However, many of my other modules are missing from that Python—and UNO is the only one missing from my Python.

Is there any way that I can install pyuno for my local Python? The LibreOffice source tree includes a pyuno/ source tree, but I'm not sure how to go about building/integrating this into another Python tree.

Any experiences here? Help? Hints? Dos, Don'ts, Dohs?

EDIT The answer below works just fine for Linux, and I have no problem there extending the PYTHONPATH to import uno. Matters are different on the Mac, so take a look at the other answer.

EDIT Absolutely take this anwer into consideration when tinkering with Python paths!


Solution

  • Linux

    dirkjot's answer to this thread works great on Linux.

    Mac (Yosemite)

    Things are a little bit more tricky here, and as of LibreOffice 4.3 I still can't extend my PYTHONPATH to LibreOffice and import uno without crashing on Mac:

    localhost ~ > PYTHONPATH=$PYTHONPATH:/Applications/LibreOffice64.app/Contents/MacOS python3.3
    Python 3.3.6 (default, Nov 12 2014, 18:18:46) 
    [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import uno
    Segmentation fault: 11
    

    But here is what works for me. First, I have to make sure that both Python and my LibreOffice are built for 32b or 64b; they can't be mixed. I'm working with 64b MacPorts Python 3.3 and 64b LibreOffice for Mac (download link) which comes with Python 3.3. Second, I have to make sure to run the right Python and extend the PYTHONPATH correctly. Because I can't run my MacPorts Python and extend it with LibreOffice's path, I have to do it the other way around: run the LibreOffice Python and extend it with my MacPorts Python path:

    localhost ~ > PYTHONPATH=$PYTHONPATH:/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages /Applications/LibreOffice64.app/Contents/MacOS/python
    Python 3.3.5 (default, Dec 12 2014, 10:33:58) 
    [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.51)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import uno
    >>> import lxml
    >>> 
    

    Note how uno is imported from the LibreOffice's Python path, and lxml lives in MacPort's Python path.