Library easymacro.py

easymacro.py it’s a library for easily develop macros en LibreOffice con Python. It is an abstraction layer between the extensive and complex LibreOffice API UNO and your code.

Probably, your will be more happy if used it. :)

You can used easymacro.py with any extension or directly in your macros.

1) Tools

1.1) For debug

INFO_DEBUG

Show info debug, show in message box.

If you have any problem in your code, you can open issue in this project, always copy the information of INFO_DEBUG in your ticket.

import easymacro as app

def info():
    app.msgbox(INFO_DEBUG)
    return

Show in shell.

import easymacro as app

def info():
    app.debug(INFO_DEBUG)
    return

Log error

Show error message in shell.

import easymacro as app

def error():
    msg = 'My error 500'
    app.error(msg)
    return

Log debug

Show debug message in shell.

import easymacro as app

def error():
    msg = 'Verify this data...'
    app.debug(msg)
    return

Log info

Show info message in shell.

import easymacro as app

def error():
    msg = 'Start process...'
    app.info(msg)
    return

Log to file

Save log to file, automatic add date and time.

import easymacro as app

def log():
    app.save_log('/home/mau/log.txt', 'PyUNO')
    app.save_log('/home/mau/log.txt', app.INFO_DEBUG)
    return

Message box

Show any data in message box

import easymacro as app

def message():

    msg = 'Please, save the planet'
    app.msgbox(msg)

    msg = ('one', 2, 'three')
    app.msgbox(msg)

    msg = {'name': 'Teresa'}
    app.msgbox(msg)

    app.msgbox(app)

    return

Catch exceptions

Sometimes, for difficult errors, you can catch exceptions.

import easymacro as app

@app.catch_exception
def test():
    r = 1 / 0
    return

And not, not used you this function in production.

Call MRI

MRI is the better extension for debug any object in LibreOffice, you need install before call it.

import easymacro as app

def error():
    obj = app.active
    app.mri(obj)
    return

1.2) Tools

Remember, always import library.

import easymacro as app

Info from PC

  • Operate system

app.msgbox(app.OS)
  • Current user

app.msgbox(app.USER)
  • Name PC

app.msgbox(app.PC)
  • Name desktop, only GNU/Linux

app.msgbox(app.DESKTOP)
  • Language

app.msgbox(app.LANG)
  • Language with variant

app.msgbox(app.LANGUAGE)
  • Application name

app.msgbox(app.NAME)
  • Application version

app.msgbox(app.VERSION)
  • In Windows

app.msgbox(app.IS_WIN)

Message box

app.msgbox(app.IS_WIN, 'My Macro')

Show warning

message = 'Caution, this action is dangerous'
title = 'My App'
app.warning(message, title)

Show error box

message = 'ERROR: Contact technical support'
title = 'My App'
app.errorbox(message, title)

Make question

message = 'Is easy Python?'
title = 'My App'
result = app.question(message, title)
app.msgbox(result)

InputBox

  • Normal data

message = 'Type your name'
default = ''
title = 'My App'

result = app.inputbox(message, default, title)
app.msgbox(result)
  • Private data

message = 'Type your password'
default = ''
title = 'My App'
echochar = "*"

result = app.inputbox(message, default, title, echochar)
app.msgbox(result)

Create instances

  • Instances without context

toolkit = app.create_instance("com.sun.star.awt.Toolkit")
  • Instances with context

service = 'com.sun.star.awt.DialogProvider2'
dialog = app.create_instance(service, True)

Paths and files

  • Get info path

path = '/home/mau/myfile.ods'
p = app.paths(path)

app.debug(p.path)
app.debug(p.file_name)
app.debug(p.name)
app.debug(p.ext)
app.debug(p.url)

Or get information in a tuple

path = '/home/mau/myfile.ods'
p = app.paths(path)

app.debug(p.info)
  • Get path home

path = app.paths.home
app.debug(path)
  • Get path documents

path = app.paths.documents
app.debug(path)
  • Get path temp

path = app.paths.temp_dir
app.debug(path)