modifying open office spreadsheet (with OO installed)

News123 news1234 at free.fr
Thu Apr 29 16:31:16 EDT 2010


Mumbling to myself, perhaps somebody else is interested.

News123 wrote:
> Hi,
> 
> 
> I wanted to know who can recommend a good module/library, that allows to
> modify an Open Office spreadsheet.
> 
> One can assume, that Open Office is installed on the host.
> 
> 
Following url gives a small introduction about using the PyUno-bridge to
open-read-modify-save a spread sheet
http://stuvel.eu/ooo-python




A simple sample code snippet could look like:
( Danny.OOo.OOoLib from above url )

import os
import uno
import unohelper
import Danny.OOo.OOoLib as OOoLib

#must start open office with
#soffice "-accept=socket,host=localhost,port=8100;urp;"
fname = "tst.ods"
new_fname = "tstnew.ods"
url = unohelper.systemPathToFileUrl(
        os.path.abspath(fname))
desktop = OOoLib.getDesktop()
doc = desktop.loadComponentFromURL(
        url, "_blank", 0, () )

sheet = doc.getSheets().getByIndex(0)
cell = sheet.getCellByPosition(0,0)
text_value = cell.getFormula()
if text_value > 3:
   new_val = "was greater three"
else:
   new_val = "was not greater three"
celltochange = sheet.getCellByPosition(1,0)
celltochange.setFormula(new_val)
url = unohelper.systemPathToFileUrl(
        os.path.abspath(new_fname))
doc.storeToURL(url, ())
doc.close(True)






More information about the Python-list mailing list