Using LibreOffice Python Macros with Recoll

The Recoll Python extension works fine with LibreOffice Python macros.

Recipee:

  • Install the "python script provider for LibreOffice" if your distributions provides it as a separate package (e.g. Ubuntu).

  • Install the APSO LibreOffice extension, which allows to create Python macros. Curiously, Python macros are supported by the base Libreoffice, but the standard macro management dialog will not let you create one…​

  • Allow macro execution in the security preferences. Tools→Preferences→LibreOffice→Security→Macro Security

  • Then use Tools→Macros→Organize Python Scripts to create the script. The APSO page above has some information about the details, and how to use the debugger, etc.

  • As an example, the following sample script works for me. It retrieves a string value from a cell, encloses it in double quotes to run a phrase search (more code would be needed in the case where the cell could itself contain double quotes), then queries the recoll db, and inserts the Xapian query string in some cell and the first result title in some other. You may want to have a look at the Recoll Python extension documentation.

# coding: utf-8
from recoll import recoll

def myfirstmacro():
	db = recoll.connect()
	q = db.query()

	doc = XSCRIPTCONTEXT.getDocument()
	cell1 = doc.Sheets[0]['A1']
	s = cell1.getString()

	r = q.execute('"' + s + '"')
	d = q.fetchone()

	cell2 = doc.Sheets[0]['A2']
	cell2.setString(q.getxquery())

	cell3 = doc.Sheets[0]['A3']
	cell3.setString(d.title)

	return