This tutorial will show you how to create a basic python script that interacts with a libre office calc spreadsheet using the APSO extension.
This tutorial is based on version 6.3.4.2.0 of Libre Office on Fedora 31.
APSO (Alternative Script Organizer for Python) Allows us to manage and attach python scripts from within Libre Office.
- Download the extension from: https://extensions.libreoffice.org/extensions/apso-alternative-script-organizer-for-python
- Open Libre Office
- Tools -> Extension Manager
- Cick on Add
- Open the APSO extension(apso.oxt)
- Click on APSO and press Check for Updates
- Choose restart now
- Tools -> Extension Manager
- Select APSO
- Click on Options
- Under apso -> EditorKicker -> Editor select your editor
I use Visual Studio Code and 'code' is the name when oppening it from the command line.
- Tools -> Options
- LibreOffice -> Security
- Click on 'Macro Security...' button
- Select Low
- Click OK button
- Click Apply button
- Click OK button
WARNING It is not wise to leave Macro Security on low, it may be ok for a quick test but I would make sure to turn it back afterwards. You could try Medium so it give you an option to run it, but it did not seem to work for me.
Here we will create a python script using APSO and attach it to the current spread sheet we are editing.
- Tools -> Macros -> Organize python scripts
- Select the file name and click on Menu -> Create Module
- Name your Module and press OK
- Expand the file name and select your module
- Click Menu -> Edit
We can now edit and save the python script and it will remain attached to the Libre Office Calc file we created. If you want to make a global script then just create it in one of the other locations.
In our script we want to create a function that performs a simple opperation.
Open the script file and you should see the following lines:
# coding: utf-8
from __future__ import unicode_literals
Add in the following lines and call the function whatever you want:
# coding: utf-8
from __future__ import unicode_literals
def TestFunction(*args):
desktop = XSCRIPTCONTEXT.getDesktop()
model = desktop.getCurrentComponent()
sheet = model.Sheets.getByIndex(0)
The first 2 lines are pretty standard for all scripts and the third gets the sheet that you want to edit. There are a few different ways you can get the sheet you want to work on:
- Get by index
model.Sheets.getByIndex(0)
- Get by name
model.Sheets.getByName("Sheet 1")
- Get by current sheet
model.CurrentController.ActiveSheet
Now lets get some values from the sheet:
cell1 = sheet.getCellRangeByName("B1").Value
cell2 = sheet.getCellRangeByName("B2").Value
We can then do something with the values we have taken and then put that back into a cell like so:
result = cell1 * cell2
sheet.getCellRangeByName("B3").Value = result
The final script will look something like this:
# coding: utf-8
from __future__ import unicode_literals
def TestFunction(*args):
desktop = XSCRIPTCONTEXT.getDesktop()
model = desktop.getCurrentComponent()
sheet = model.Sheets.getByIndex(0)
cell1 = sheet.getCellRangeByName("B1").Value
cell2 = sheet.getCellRangeByName("B2").Value
result = cell1 * cell2
sheet.getCellRangeByName("B3").Value = result
- View -> Toolbars -> Form Controls
- Enable Design mode
- Click on 'Push Button'
- Drag out The button
- Right click on the button and select 'Control Properties...'
- Rename the button
- Click on the Events tab
- Click on the '...' button next to 'Mouse button pressed...'
- Under Assign Click on 'Macro...'
- Browse to your script file and select the function you want
- Click OK and close the properties window.
- Disable design mode and close the tool bar
You should now be able to click on the button you created to run your script.
You can get the test file I created here:
ExamplePythonScripting.ods
These are the resources I used to make this tutorial.