I am finally starting to write this tutorial. Thanks for your patience, and thanks to Nukool for your comment, it really gave me motivation to write this course.
In this Part 1 of LibreOffice Calc & Python programming tutorial we are looking at:
- some Software you need
- the APSO extension that’ll make things much easier
- an extra step for Linux users
- useful documentation
- further notes
You will of course need LibreOffice, you can download the latest version here.
LibreOffice is normally shipped with a PyUNO bridge to bind its components with Python language and to make it possible to program LibreOffice with Python. (see at end of this article if you’re using a Linux ).
You also need the Python 3 programming language on your computer.
It may be installed already. For now I let you do it yourself, but I might write a quick help later. Just web search for ‘python 3’ for your Operating system.
IDE / Text Editor
You will also need a text editor or even better an IDE (Integrated Development Environment) to write your python program. The advantage of an IDE versus a simple text editor is that it offers tools to organise your codes, an IDE can also makes it easier to spot errors and offers all sort of features to speed up and ease coding like word auto-completion, search and replace.
If you are a programmer you may already have a preference for a simple editor or a clever one. I am using a free open-source IDE called geany, but other popular ones are Eclipse, code::block and the famous Microsoft Visual Studio. They all have their pros and cons, here is the Top 20 of the most popular ones. Geany is perfect for ‘small jobs’ like we are going to do, but again it’s also fine to use a text editor like notepad.
If your computer does not have Java Runtime Environment already you will need to install it through the Oracle website. You can also follow instruction here. JRE is indeed required to be able to use macros within LibreOffice.
By default LibreOffice Python Macros are independent from any LibreOffice file. So to execute a python program within a LibreOffice Calc Spreadsheet for example you would need to go in Tools/Macro/Run Macro and select the python program from a list corresponding to a folder on your Hard drive. And this list, for example ‘MyMacro’, would be accessible for any LibreOffice file (Calc, Text, Base, etc…). This is great but not helpful when you want a particular python program to be used specifically for a particular file.
APSO is a LibreOffice extension that helps with organising Python Macros within your LibreOffice files. You just need to download the APSO extension here and then install the tool via Tools/Extensions Manager. One great advantage of APSO is its EditorKicker that you can set up to use your preferred text editor or IDE to edit your Python program.
One last note about using APSO, although the program themselves are saved in the same folders than all other programs, the ones you choose to be embed in a particular file will be linked to that file, or rather for example LibreOffice calc file will have a link to the python program in its parameters and you will be able to see your program attached to LibreOffice documents currently opened.
Extra step for linux
I had to do one extra step when I was using LibreOffice 5 under Linux mint Operating System: adding some library. Without it it was not possible to do any Python macros within LibreOffice. This is an easy step, just open a terminal, type and execute:
sudo apt-get install libreoffice-script-provider-python
It will be the same for Ubuntu. For other linux distro you will need to check yourself but it’d be similar using the in-house package manager.
It is not easy to find helpful documentation for programming Python Macros for LibreOffice. Firstly it’s hard to find a site or forum that have real documentation about using Python for LibreOffice, and secondly the tasks explained are usually too complicated for a beginner. However this is the best place I found so far with a list of links to Python program examples. You will not that most of the examples are using a Python library created by ‘Danny’.
In my tutorial we will create everything from scratch and develop simple and useful functions.
I still haven’t decided what topic to cover to illustrate this tutorial. I am hesitating between continuing the time schedule, starting invoice manager or a garden planner.
If you have any preference please let me know in your comment.