Writing / reading datasets in Open Office using Python. Does anyone have some sample code?

So, I wrote a class that makes it extremely easy to interact with Excel or Gnumeric using Python, and would like to extend the class to include Open Office. I could have done this in 30 minutes if I had the opportunity to do the following:

  • Set one value on an arbitrary sheet and workbook
  • Get one value on an arbitrary sheet and book

If they are slow / there is a way to do the following, I also need to be able to:

  • set / get array '' '
  • set / get matrix '' '

ALSO, the ability to create and rename sheets would be great.

It is a cry if someone has worked on this before. If they give me information, I will refer to them at the top of the file

My project can be found here: https://sourceforge.net/projects/pyworkbooks/ and I recommend that you check it out.

+6
python excel openoffice-calc
source share
4 answers

In fact, in order to get OpenOffice or LibreOffice through Python, you need to go through an absolutely opaque amount of boiler plate inherited from StarOffice - it has never been properly documented (felt) or simplified since then.

I once lectured on this and I took almot 40 minutes just to get parts of my lecture to set up the example below.

On the other hand, it just worked with the latest version of LibreOffice - 3.3 - I'm sure it works for OpenOffice too (but I would not advise anyone to stick with OpenOffice, at the moment this is an Oracle deadlock)

The following example uses the slow method to connect to an executable instance of LibreOffice from the outside. This is very slow - you will need to refer to the documentation on how to make it work as a macro from within the program to improve performance. (it really is so slow).

However, this method allows you to learn the methods available to developers using the Python terminal and introspection.

The first poorly documented part is that you need to run Open / LibreOffice with: soffice "-accept=socket,host=0,port=2002;urp;" For compounds to be accepted. Then create a new table through your interface and with the python interpreter that comes with Office Suite, execute the following code (either interactively or as a script):

 import uno import socket # only needed on win32-OOo3.0.0 # get the uno component context from the PyUNO runtime localContext = uno.getComponentContext() # create the UnoUrlResolver resolver = localContext.ServiceManager.createInstanceWithContext( "com.sun.star.bridge.UnoUrlResolver", localContext ) # connect to the running office ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" ) smgr = ctx.ServiceManager # get the central desktop object desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx) # access the current writer document model = desktop.getCurrentComponent() try: sheets = model.getSheets() except Exception: raise TypeError("Model retrived was not a spreadsheet") sheet1 = getattr(sheets, sheets.ElementNames[0]) # At this point, you can use "dir" to check the methods and # attributes available for the sheet # the methots "getCellByPosition, to retrieve a cell object, # which has "getFormula" and "setFormula" # methods. for i in xrange(10): for j in xrange(10): cell = sheet1.getCellByPosition(i, j) cell.setFormula(str(i * j)) c1 = sheet1.getCellByPosition(1,1) 

As you can see, the connecting part of this template I got somewhere else a few years ago, and I doubt that any living person can find any justification in such things. However, when you go to the "sheets" object, the attributes and methods of the object begin to make sense.

The online guide provides a complete guide for developers to even understand part of the connection:

http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/OpenOffice.org_Developers_Guide

+8
source share

The Inter-process interface for connecting to LibreOffice (as well as OpenOffice and StarOffice) is called UNO. It is documented on the LibreOffice API documentation page .

As jsbueno says, he expects the daemon to work for communication. The command line arguments for the soffice command that starts the daemon determine which host and port values ​​you need to provide in your UNO calls.

+1
source share

You can also try ezodf. It was the best odyth python library I found.

+1
source share

You can use pyoo . Here is my answer to a similar question / questions / 884427 / can-python-read-the-value-of-a-cell-in-a-spreadsheet / 3169667 # 3169667

0
source share

All Articles