A recent study on the use of Python to write data to the LibreOffice spreadsheet, the information on the OpenOffice website is not very clear,
Some of the more useful websites found on the Internet are: http://wiki.services.openoffice.org/wiki/Python#PyUNO_Modules
There's a better getting started document: Http://lucasmanual.com/mywiki/OpenOffice
There is one more useful example of a person who writes on the Forum: http://www.oooforum.org/forum/viewtopic.phtml?p=56037#56037
This example is very good, tells you how to operate spreadsheet, I did not find in other places the Operation spreadsheet document, if which Netizen has found, leaves a message, thanks.
Now put the basic code on the other, please check the above two URLs can be
First you start libreoffice with the following command:
Libreoffice-accept= "SOCKET,HOST=LOCALHOST,PORT=2002;URP;"
Start OpenOffice So it listens on a port.
Connection:
Import Uno
def Connect ():
local = Uno.getcomponentcontext ()
Resolver = Local. Servicemanager.createinstancewithcontext ("Com.sun.star.bridge.UnoUrlResolver", local)
Context = Resolver.resolve ("UNO:SOCKET,HOST=LOCALHOST,PORT=2002;URP; Staroffice.componentcontext ")
desktop = context. Servicemanager.createinstancewithcontext ("Com.sun.star.frame.Desktop", context)
return Desktop
Class Calc:
def __init__ (self, file_name = "Private:factory/swriter"):
Desktop = connect ()
Self.doc = Desktop.loadcomponentfromurl (file_name, "_blank", 0, ())
Self.sheet = Self.doc.getSheets (). Getbyindex (0)
def get_dic (self, start, end):
For row in range (start-1, end):
Value = self.sheet.getCellByPosition (0, row). GetString ()
Self.sheet.getCellByPosition (1, row). SetString ("have read")
def save (self):
Self.doc.store ()
def main ():
Calc_file = "File:///home/zhangliyong/Desktop/2011.xls"
Calc_obj = Calc (calc_file)
Calc_obj.get_dic (5, 46)
Calc_obj.save ()
Note When you manipulate the LibreOffice document, precede the document path with file://