pythonmacroslibreoffice-calc

Can't create a python macro in Libre Office Calc


I have read tutorials that explain how to create a python macro :

{ Code :

from scriptforge import CreateScriptService

def test(args=None)
    doc = CreateScriptService("Calc")
    doc.SetValue("G3","test")
    return None

}

But when I open the popup Macros Python, the folder break is empty :

Popup when I open Tools - Macros - Manage Macros - Python

I'd like to know why, or at least to know where are stored all these "Application macros", so I can have a look at them.

I use Libre Office version 24.2.3.2 (X86_64) on Windows 10.0


Solution

  • Open a PowerShell prompt and execute the script with LibreOffice's python.

    chdir "${env:ProgramFiles}\LibreOffice\program\"
    ./python "C:/Users/U/AppData/Roaming/LibreOffice/4/user/Scripts/python/break.py"
    

    This reveals the error, which as you can see, is due to missing : after the statement.

    File "C:/Users/U/AppData/Roaming/LibreOffice/4/user/Scripts/python/break.py", line 3
        def test(args=None)
                          ^
    SyntaxError: invalid syntax
    

    On Windows, the "Application" (shared) macro location is typically C:\Program Files\LibreOffice\share\Scripts\python.

    If you haven't yet, be sure to try the APSO extension to run python-uno macros. And the tutorial.

    For bigger projects, I use pylint to check syntax. Also, I catch and log exceptions, for example:

    def log_exceptions(self, func):
        """Decorator method to log uncaught exceptions."""
        def wrapper(*args, **kwargs):
            try:
                func(*args, **kwargs)
            except:
                self._setup()
                self.logger.exception("Caught exception at top level.")
                # Re-raising is proper coding practice when catching all
                # exceptions, and we do so even though it will probably
                # have no effect, at least during runtime.
                raise
    
        return wrapper
    
    def _setup(self):
        """Set up a minimalist file logger."""
        raise NotImplementedError()