Macros/Python Guide/Common errors
TDF LibreOffice Document Liberation Project Community Blogs Weblate Nextcloud Redmine Ask LibreOffice Donate
Common errors
Please, Python is not Basic, this is good.
Indentation
The most common error when starting to develop macros in Python or switching from Basic is the indentation. Python is very strict with syntax.
If this works in Basic
Sub test()
If True Then
MsgBox "Hello"
End If
End Sub
This does not work in Python
def test():
if True:
print('Hello')
return
The correct version is:
def test():
if True:
print('Hello')
return
Many instructions in Python are determined by the indentation. In other programming languages the indentation in code is for readability only, the indentation in Python is very important and validates many logical instructions.
Tabulations or spaces
You can use tabulations or spaces for the indentation. The PEP 8 recommends using spaces, but not combining them with tabs. Python 3 disallows mixing the use of tabs and spaces for indentation and please do not use Python 2 anymore.