Search code examples
headlesslibreoffice-calc

How can I open LibreOffice Calc and close it in headless mode


I have .iqy file which is Internet query file for Excel. If I open that file with LibreOffice Calc I can get the sheet correctly populated with the data and I can save it as I please with GUI.

My question is how to open that file in headless mode in a way that I can save the populated file as .xls file? Preferred solutions would be either bash scripting or Python because I could easily implement them in my current project.

The steps I want to do in headless mode are as following:

  1. Open .iqy file with LibreOffice Calc
  2. Wait for spreadsheet to populate from data
  3. Save As .xls

I can make them easily with GUI, but on server I am working I should be able to do these things without GUI via scripting.

EDIT: It seems that there is no other way than try to do it via LibreOffice API. I post update here if I manage to find workaround.


Solution

  • I managed to make this work with python and uno. Below is my code

    import uno
    from com.sun.star.beans import PropertyValue
    
    def convert_iqy_excel(xls_path, socket="socket", host="localhost", port="2002"):
        # 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:{},host={},port={};urp;StarOffice.ComponentContext".format(socket,host,port))
        smgr = ctx.ServiceManager
    
        # get the central desktop object
        desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
    
        # access the current calc document
        doc = desktop.getCurrentComponent()
    
        # save as f_name
        f_name = "file://{}".format(xls_path)
    
        args = (PropertyValue('FilterName', 0, 'MS Excel 97', 0),)
    
        doc.storeToURL(f_name, args)
    
        # Do a nasty thing before exiting the python process. In case the
        # last call is a oneway call (e.g. see idl-spec of insertString),
        # it must be forced out of the remote-bridge caches before python
        # exits the process. Otherwise, the oneway call may or may not reach
        # the target object.
        # I do this here by calling a cheap synchronous call (getPropertyValue).
        ctx.ServiceManager
        doc.dispose()
    

    I have LibreOffice 6.0.7.3 and Python 3.6.7