Python Operations LibreOffice Spreadsheet__python

Source: Internet
Author: User

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://

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.