Search code examples
pythonmacrospycharmopenoffice.orglibreoffice

How to configure PyCharm to develop LibreOffice Python macros?


I've installed Python 3.5.1 as default in Win7(x64) for all my projects in Python.

I use PyCharm 5.0.5 community edition for develop Python scripts and its default settings has "Default Project Interpreter" as "3.5.1 (C:\Python35\python.exe)"

At my work we are migrating from MS Office 2007/2010 to LibreOffice-5. I wrote some macros in VBA, despite of I'm not a VB enthusiastic. Basic lacks good data structures, such as lists (I love list comprehensions), dictionaries, sets and tuples. So, I want to rewrite the VBA macros in LibreOffice-5 Python script macros.

LibreOffice-5 installation has its own embebed Python at "C:\Program Files (x86)\LibreOffice 5\program", version 3.3.5. Scripts of Python in LibreOffice-5 installation is at :

  • Libre Office Macros; "C:\Program Files (x86)\LibreOffice 5\share\Scripts\python"
  • My Macros; "C:\Users\trimax\AppData\Roaming\LibreOffice\4\user\Scripts\python"

The question is simple:
I need configure PyCharm settings to develop the python scripts of LibreOffice macros with the embebed python version. I don't know if I need to set a virtual environment or if I can just to set the Project Interpreter.

By the way, are there any method to insert macros in the document, to share it with the document, as the VBA Project Modules?


Solution

  • From the PyCharm documentation, it sounds like you could use a virtual environment to target LibreOffice (likely Python 3) and OpenOffice (likely Python 2) in two different projects. Otherwise it looks like a local interpreter is enough.

    To test PyCharm, I did the following:

    1. Download PyCharm and create a new project.
    2. It asks which interpreter to use. Click on the gear icon and specify Add Local. Browse to C:\Program Files (x86)\LibreOffice 5\program\python.exe.
    3. Create a new python file.

    Then add this code:

    import uno
    from com.sun.star.awt import Point
    
    p = Point(2,3)
    print(p.X)
    points = uno.Any("[]com.sun.star.awt.Point", (p,))
    print(repr(points))
    

    It underlined the com import statement, although it's not actually an error. PyCharm did recognize the other statements such as uno.Any.

    To run, go to Run -> Run. It ran successfully and printed results as expected.

    Instead of an IDE, I typically just use a text editor. From what I have seen, a lot of the IDE tools (syntax highlighting, auto completion, debugging) do not work very well with UNO anyway. It is better with Java, but that is a different topic.

    By the way, are there any method to insert macros in the document, to share it with the document [...]?

    To embed Python code into a document, unzip the .odt file and follow the instructions here.