Macros/Python Guide/Common errors

From The Document Foundation Wiki
Jump to navigation Jump to search
This page contains changes which are not marked for translation.

Return to Index

Other languages:
English • ‎español • ‎italiano • ‎日本語

Common errors

Please, Python is not Basic, this is good.


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:

The correct version is:

def test():
    if True:

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.