Package: unotools
UnoTools allows you to interact with OpenOffice.org/LibreOffice using
the “UNO bridge”. The aim is to make handling OpenDocument easy than
using the original UNO/PyUNO for scripting.The unotools is quite simple, but you ought to understand UNO APIs.The Apache OpenOffice API
DocumentUNOPyUNOThere’re other tools.pyoounoconvHow to installRequirementsOpenOffice.org/LibreOffice 3.4 or latorPython 3.3 or latorOn Ubuntu 14.04Install libreoffice, uno library and python3:$sudoaptitudeinstall-ylibreofficelibreoffice-script-provider-pythonuno-libs3python3-unopython3I like virtualenvwrapper to make temporary environment:$sudoaptitudeinstall-yvirtualenvwrapper$mkvirtualenv-p/usr/bin/python3.4--system-site-packagestmp3Confirm importing uno module:(tmp3)$pythonPython3.4.0(default,Apr112014,13:05:11)[GCC4.8.2]onlinuxType"help","copyright","credits"or"license"formoreinformation.>>>importunoInstall unotools from PyPI:(tmp3)$pipinstallunotoolsOn Mac OS XDownload LibreOffice DMG package fromhttps://www.libreoffice.org/and
install.$hgclonessh://hg@bitbucket.org/t2y/unotoolsIn Python 3.3 case, need singledispatch package. Confirm Python
interpreter version included in LibreOffice.$hgclonessh://hg@bitbucket.org/ambv/singledispatchSetPYTHONPATHto resolve additional packages in LibreOffice’s Python
interpreter.$exportPYTHONPATH="/path/to/singledispatch/:/path/to/unotools/"On Mac OS X, soffice and python commands are as below.$/Applications/LibreOffice.app/Contents/MacOS/soffice--versionLibreOffice4.4.0.3de093506bcdc5fafd9023ee680b8c60e3e0645d7Confirm importing unotools package:$/Applications/LibreOffice.app/Contents/MacOS/pythonPython3.3.5(default,Jan222015,17:12:45)[GCC4.2.1CompatibleAppleLLVM6.0(clang-600.0.51)]ondarwinType"help","copyright","credits"or"license"formoreinformation.>>>importunotoolsHow to use (on Ubuntu 14.04)Startup libreoffice:(tmp3)$soffice--accept='socket,host=localhost,port=8100;urp;StarOffice.Service'Download sample-scripts fromhttps://bitbucket.org/t2y/unotools/raw/default/sample-scripts:(tmp3)$pythonsample-scripts/writer-sample1.py-slocalhost(tmp3)$pythonsample-scripts/calc-sample1.py-slocalhost-dsample-scripts/datadir/(tmp3)$lssample-calc.htmlsample-calc.pdfsample-calc_html_eaf26d01.pngsample-scriptssample-writer.htmlsample-writer.pdfsample.csvsample.docsample.odssample.odtsample.xlsThere’s a sample script to convert odt/ods to pdf.(tmp3)$pythonsample-scripts/pdf-convert-sample1.py-slocalhost-fsample.odt(tmp3)$pythonsample-scripts/pdf-convert-sample1.py-slocalhost-fsample.ods(tmp3)$lssample.pdfLook through these sample script, then it help you how to use unotools.Interact with interpreter step by stepInteracting documents with UNO make you learn how to use unotools.Startup LibreOffice:$soffice--accept='socket,host=localhost,port=8100;urp;StarOffice.Service'>>>fromunotoolsimportSocket,connect>>>fromunotools.component.writerimportWriter>>>context=connect(Socket('localhost',8100))>>>writer=Writer(context)Now, you can see new document window on LibreOffice.>>>writer.set_string_to_end('Hello\n')>>>writer.set_string_to_end('World\n')Then,HelloandWorldare put into the document window.>>>text=writer.text>>>text<unotools.component.writer.Textobjectat0x1064a8e10>Writer inheritsXTextRangeinterface and has those methods. To make full use of unotools, you have
to understand UNO APIs.There’s a tip to confirm what methods are exist in the component. The_show_attributes()is a helper method that unotools component
inherit.>>>text._show_attributes()[...'getElementType','getEnd','getImplementationId','getImplementationName','getPropertySetInfo','getPropertyValue','getSomething','getStart','getString','getSupportedServiceNames','getText','getTypes',...]Though these methods are CamelCase, you can also invoke Python style
methods.>>>text.getString()'Hello\nWorld\n'>>>text.get_string()'Hello\nWorld\n'Both are same method and unotools component handles to convert Python
style to original method.Let’s save this document asodt.>>>fromunotools.unohelperimportconvert_path_to_url>>>url=convert_path_to_url('./test1.odt')>>>url'file:///Users/t2y/work/repo/unotools/test1.odt'>>>writer.store_to_url(url,'FilterName','writer8')>>>writer.close(True)If you want to read OpenDocument on file system, like this.>>>writer=Writer(context,convert_path_to_url('./test1.odt'))ChangeLog0.3.3 (2015-03-21)added PDF conversion sample scriptupdated README as tutorial0.3.2 (2014-06-07)added some classes for calcchanged data directory option of command line argument (-d,
–datadirs) to be able to store multiple directories0.3.1 (2014-05-07)added command line argument (-f, –file) to be able to read
OpenDocument file0.3.0 (2014-05-05)first release