Python Macro in LibreOffice(1)-How to use and Sample Code

Published on:
Last updated:

Sponsored Links

This post is also available in: Japanese

Write down contents as article when our company developed macro in LibreOffice.
Most of contents is Windows environment and using Python in LibreOffice Calc.

Security settings in Calc

You can find menu Start up Calc > Tools > Option
And change security settings as below image.

libre1-001

Install JRE(Java Runtime Environment)

You can find menu Tools > Macro > Organize Macros > Python, and run sample script as first test.
Depending on the environment, it may cause error like below.

LibreOffice requires a Java runtime environment (JRE) to perform this task…etc

If you don’t install JRE(Java Runtime Environment) in your PC, install it.
Maybe if you already installed JRE in your PC, you can select version of JRE from menu Tools>Options>LibreOffice>Advanced.
In my situation, JRE 1.8 is causing error but JRE 1.7 is going good. My PC is 64bit environment, so JRE install path is ’Program Files(x86)’.

Running Sample Python Macro

You can find menu Tools>Macro>Organaize Macros>Python, and run sample macro script.

libre1-003

I made ‘gettoday.py’ like below, and copy to foler path as below. Then run code from menu Tools>Macros>Organize Macros>Python, resluts are displayed in Calc.

Sample scripts are in folder path as follow if you don’t change default install path.

C:\Program Files (x86)\LibreOffice 5\share\Scripts\python

#gettoday.py
import sys
import datetime
def PrintToday( ):
	#get the doc from the scripting context which is made available to all scripts
    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()
	#check whether there's already an opened document. Otherwise, create a new one
    if not hasattr(model, "Sheets"):
        model = desktop.loadComponentFromURL(
            "private:factory/scalc","_blank", 0, () )
	#get the XText interface
    sheet = model.Sheets.getByIndex(0)
	#create an XTextRange at the end of the document
    tRange = sheet.getCellRangeByName("A2")
	#and set the string
    tRange.String = "Today is (in Python) " + str(datetime.date.today()) + " !!"
    #do the same for the python executable path
    tRange = sheet.getCellRangeByName("A3")
    yesterday = datetime.date.today() - datetime.timedelta(1)
    tRange.String = "Yesterday was (in Python) " + str(yesterday) + " !!"
    return None

In the same way, you can display results to LibreOffice Writer from menu Tools>Macros>Organized Macros>Python by using sample macro scirpt as follows.

#gettoday-writer.py
import sys
import datetime
def PrintToday( ):
	#get the doc from the scripting context which is made available to all scripts
    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()
    #check whether there's already an opened document. Otherwise, create a new one
    if not hasattr(model, "Text"):
        model = desktop.loadComponentFromURL(
            "private:factory/swriter","_blank", 0, () )
	#get the XText interface
    text = model.Text
	#create an XTextRange at the end of the document
    tRange = text.End
	#and set the string
    tRange.String = "Today is (in Python) " + str(datetime.date.today()) + " !!"
    return None

About
Kuniyoshi Takemoto is the founder of Amelt.net LLC, and editor of this blog(www.amelt.net).Learn more and connect with me on Google+,LinkedIn(English).


Sponsored Links