First let’s get the coding done, then we’ll dive in to it.
# Created by Gweno 16/08/2019 for tutolibro.org
# This program displays 'Hello World!" in cell A1 of the
# current Calc document.
# get the doc from the scripting context
# which is made available to all scripts
desktop = XSCRIPTCONTEXT.getDesktop()
model = desktop.getCurrentComponent()
def HelloWorld():
"""Write 'Hello World!' in Cell A1"""
# access the active sheet
active_sheet = model.CurrentController.ActiveSheet
# write 'Hello World' in A1
active_sheet.getCellRangeByName("A1").String = "Hello World!"
You can either copy this block of code above or better type it yourself. Typing the code yourself will take longer but it’s the best way to remember a new language.
The first lines starting with ‘#’ are comments.
A comment in a program is a line that is not processed when the program is executed. It helps the person reading the program to understand what the code is doing. Most of the time, this person is yourself. Comments will help you remember what you were trying to achieve with this code.
The blank space at the start of indented lines should be made of 4 spaces: active_sheet = model.CurrentController.ActiveSheet
If you don’t do that it will create an error because the Python interpreter will not recognise the line as being part of the function define by:def HelloWorld():
The instruction ‘def’ is to ‘define a function’. A function is a set of written instructions. It can be run on its own. It can also be ‘called’ by other functions to be executed.
Our function here is called ‘HelloWorld’. The part between parenthesis ‘()’ is where you write the arguments to be used by the function. In our case we are not ‘passing’ any argument to the function ‘HelloWorld’ for now. That’s why we have nothing within the parenthesis ‘()’.
If you want to assign this macro to a button, you need to pass a specific argument. This argument is necessary for the macro to work when pressing the button. (see this post explains how to assign a button to a python macro)

Explanations:
Lines 1,2,3,5,6: these lines start with a ‘#’ so they are comments.
Lines 4,9,18: these empty lines are only for making the program easier to read. They serve to separate the different sets of instructions within the program. The Python interpreter does not need them but these empty lines make the code more readable for us.
Line 7:desktop = XSCRIPTCONTEXT.getDesktop()
This line is what allows us to use Python language to manipulate LibreOffice’s components. It is a bit complicated to explain exactly what this line does. In short, we define ‘desktop’ to hold the ‘XSCRIPTCONTEXT‘ interface provided by LibreOffice for our scripts (programs). The ‘getDesktop()’ function is used to obtain a reference for our scripts to operate on. (It is not a reference to a computer desktop!). (Click on the links above to read more in my ‘bitesize’ section).
Line 8:model = desktop.getCurrentComponent()
Here, we define ‘model’ to be the current component of ‘desktop’. Thus, we are referencing the current document open. It should be a Calc document.
Line 10:def HelloWorld():
Define a new function called ‘HelloWorld’ that has no arguments.
The ‘:’ at the end is essential, it’s to say that what is following is the actual code written for this function. And every line that follow this function’s 1st line should start with 4 spaces.
The function finishes when there is no lines after it. Or when the next non empty line is not a comment and starts with no space.
Line 11:
“””Write ‘Hello World!’ in Cell A1″””
This line is simply a description of the function. Also called a Docstring (DocStrings is short for Documentation Strings), it starts and ends with 3 quotation marks. It is not really useful for us right now. Still, it is good practice to have a short description for each of your functions. It’s different from a comment because this description can be displayed outside of the program as a ‘help’ note.
To display docstrings, you can use the built-in function ‘help’, like illustrated in the APSO console below:

Line 14: active_sheet = model.CurrentController.ActiveSheet
Here we are defining ‘active_sheet’ to be the current spreadsheet you are looking at.
Line 17: active_sheet.getCellRangeByName("A1").String = "Hello World!"
Finally! Now we have a reference to the current LibreOffice Context. We have the current LibreOffice Calc Document (component) and the current spreadsheet. Therefore, we can access the cell ‘A1’. We can then assign to its ‘String’ parameter the text “Hello World!
Exercise:
Add to function HelloWorld a line of code to display “Bye Bye World!” in cell ‘B2’. I am sure you can handle that!
If you like this tutorial, you could buy me a coffee to help me continue writing add-free tutorials.
Thank you!





