Well, apparently there is a way with python3
on Ubuntu 14.04; you have to use (import
) either the pyuno.so
which is included in the LibreOffice Ubuntu distribution, or the python3-uno
module - they seem to be equivalent, though. Here is a transcript of my test shell session:
$ # http://www.openoffice.org/udk/python/python-bridge.html
$ which soffice
/usr/bin/soffice
$ ls -la `which soffice`
lrwxrwxrwx 1 root root 34 Apr 29 00:53 /usr/bin/soffice -> ../lib/libreoffice/program/soffice
$ readlink -f `which soffice`
/usr/lib/libreoffice/program/soffice
$ dirname $(readlink -f `which soffice`)
/usr/lib/libreoffice/program
$ cd $(dirname $(readlink -f `which soffice`))
$ ls py*
pythonloader.py pythonloader.unorc pythonscript.py pyuno.so
# note: this process runs in the foreground of the shell - so make it run as background with &;
# it starts a LibreOffice instance, can open a document there, say calc .ods
# there is no separate python executable in this directory on Ubuntu 14.04; just pythonloader.py, pythonloader.unorc pythonscript.py pyuno.so
# ./soffice "-accept=socket,host=localhost,port=2002;urp;" # Warning: -accept=socket,host=localhost,port=2002;urp; is deprecated. Use:
$ ./soffice "--accept=socket,host=localhost,port=2002;urp;" &
[1] 22783
$ python3
Python 3.4.3 (default, Nov 17 2016, 01:08:31)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyuno
>>> dir(pyuno)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_createUnoStructHelper', 'absolutize', 'checkEnum', 'checkType', 'experimentalExtraMagic', 'fileUrlToSystemPath', 'generateUuid', 'getClass', 'getComponentContext', 'getConstantByName', 'getCurrentContext', 'getTypeByName', 'invoke', 'isInterface', 'setCurrentContext', 'systemPathToFileUrl']
>>> import uno
>>> dir(uno)
['Any', 'Bool', 'ByteSequence', 'Char', 'Enum', 'Type', '__builtin__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_g_ctx', '_g_delegatee', '_impl_extractName', '_uno_extract_printable_stacktrace', '_uno_import', '_uno_struct__eq__', '_uno_struct__getattr__', '_uno_struct__init__', '_uno_struct__repr__', '_uno_struct__setattr__', '_uno_struct__str__', 'absolutize', 'createUnoStruct', 'fileUrlToSystemPath', 'generateUuid', 'getClass', 'getComponentContext', 'getConstantByName', 'getCurrentContext', 'getTypeByName', 'invoke', 'isInterface', 'os', 'pyuno', 'setCurrentContext', 'socket', 'sys', 'systemPathToFileUrl', 'unicode']
# get the uno component context from the PyUNO runtime
>>> localContext1 = pyuno.getComponentContext()
>>> dir(localContext1)
['/services/com.sun.star.security.AccessController/mode', '/singletons/com.sun.star.configuration.Update', ...
>>> localContext2 = uno.getComponentContext()
>>> dir(localContext2)
['/services/com.sun.star.security.AccessController/mode', '/singletons/com.sun.star.configuration.Update', ...
>>> len(dir(localContext1))
61
>>> len(dir(localContext2))
61
# create the UnoUrlResolver
>>> resolver1 = localContext1.ServiceManager.createInstanceWithContext( "com.sun.star.bridge.UnoUrlResolver", localContext1 )
>>> resolver2 = localContext2.ServiceManager.createInstanceWithContext( "com.sun.star.bridge.UnoUrlResolver", localContext2 )
# connect to the running office
>>> ctx1 = resolver1.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
>>> ctx2 = resolver2.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
>>> smgr1 = ctx1.ServiceManager
>>> smgr2 = ctx2.ServiceManager
# get the central desktop object
>>> desktop1 = smgr1.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx1)
>>> desktop2 = smgr2.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx2)
# access the current document
>>> model1 = desktop1.getCurrentComponent()
>>> model2 = desktop2.getCurrentComponent()
>>> len(dir(model1))
259
>>> len(dir(model2))
259
>>> dir(model1)
['ActionLocks', 'AllVersions', 'AllowMacroExecution', 'ApplyFormDesignMode', 'AreaLinks', 'Args', ...
>>> dir(model2)
['ActionLocks', 'AllVersions', 'AllowMacroExecution', 'ApplyFormDesignMode', 'AreaLinks', 'Args', ...
>>> model1.getArgs()
((com.sun.star.beans.PropertyValue){ Name = (string)"URL", Handle = (long)0x0, Value = (any){ (string)"file:///home/USERNAME/Desktop/memleak.ods" }, State = (com.sun.star.beans.PropertyState)DIRECT_VALUE }, ...
>>> model1.getArgs()[0].Name
'URL'
>>> model1.getArgs()[0].Value
'file:///home/USERNAME/Desktop/memleak.ods'
# BASIC: ThisComponent.CurrentSelection
>>> model1.getCurrentSelection()
pyuno object (com.sun.star.uno.XInterface)0x11516c8{implementationName=ScCellObj, ... # for the default single-cell selection in calc
>>> model1.getCurrentSelection()
pyuno object (com.sun.star.uno.XInterface)0x131a0e8{implementationName=ScCellRangeObj, ... # multi-cell selection in calc
>>> cursel1 = model1.getCurrentSelection()
>>> dir(cursel1)
['AbsoluteName', 'ArrayFormula', 'ArrayTokens', 'AsianVerticalMode', 'BottomBorder', ...
>>> cursel1.Rows
pyuno object (com.sun.star.table.XTableRows)0x133bd08{implementationName=ScTableRowsObj, ...
>>> cursel1.getRows()
pyuno object (com.sun.star.table.XTableRows)0x1350138{implementationName=ScTableRowsObj, ...
>>> cursel1.Columns
pyuno object (com.sun.star.table.XTableColumns)0xf6e878{implementationName=ScTableColumnsObj, ...
>>> cursel1.getColumns()
pyuno object (com.sun.star.table.XTableColumns)0x13549d8{implementationName=ScTableColumnsObj, ...
# [http://ooo-forums.apache.org/en/forum/viewtopic.php?f=20&t=54301 [Solved] Calc - Get exact cell content with Python (View topic) • Apache OpenOffice Community Forum]
# http://christopher5106.github.io/office/2015/12/06/openoffice-libreoffice-automate-your-office-tasks-with-python-macros.html
>>> oArea1 = cursel1.getRangeAddress()
>>> oArea1
(com.sun.star.table.CellRangeAddress){ Sheet = (short)0x0, StartColumn = (long)0x8, StartRow = (long)0x14, EndColumn = (long)0x8, EndRow = (long)0x17 }
>>> oArea1.StartRow
20
>>> oArea1.EndRow
23
>>> active_sheet1 = model1.CurrentController.ActiveSheet
>>> active_sheet1
pyuno object (com.sun.star.sheet.XSpreadsheet)0x13b58d8{implementationName=ScTableSheetObj, ...
>>> oCell1 = active_sheet1.getCellByPosition(oArea1.StartColumn, oArea1.StartRow)
>>> oCell1
pyuno object (com.sun.star.table.XCell)0x13dba88{implementationName=ScCellObj, ...
>>> oCell1.String
'Hello World'