[Tutor] Open a libreoffice calc file in Python

Jim Byrnes jf_byrnes at comcast.net
Thu Dec 22 13:12:13 EST 2016


On 12/22/2016 03:54 AM, Alan Gauld via Tutor wrote:
> On 22/12/16 03:37, Jim Byrnes wrote:
>> Python 3.4 on Ubuntu
>>
>> If I was going to open a libreoffice calc file from the terminal I would
>> go: libreoffice --calc /home/path/to/myfile.ods.
>>
>> How would I do this from Python?
>
> Others have advised how to run the Libreoffice app from
> within Python.
>
> If you really want to open the actual spreadsheet file
> in Python rather than Libreoffice then its a bit more tricky.
> If that is what you really meant get back to us and
> we can start on the options available...
>
>

First thanks to everyone that responded pointing me in the right direction.

Alan,

I want to open Libreoffice with a particular file loaded.

I started out to write a libreoffice macro in python to go to a website, 
get some info, copy it to the clipboard and then paste it in libreoffice 
calc.

I started out by writing a script using Selenium that successfully did 
what I wanted. Once I had it working the plan was to put it in a macro. 
When I did that I got a large error message where both libreoffice and 
selenium complained.  I seemed to say that the first element of the 
webpage I tried to manipulate was in a state that it could not be 
interacted with, even though outside of libreoffice the script ran fine.

This got me thinking that maybe I should attack the problem from the 
other end, ie run the script and have it load libreoffice at the end. 
Hence my question.

for completeness here is the error msg I received:

com.sun.star.uno.RuntimeExceptionError during invoking function login in 
module 
file:///home/jfb/.config/libreoffice/4/user/Scripts/python/funds/funds.py 
(<class 'selenium.common.exceptions.InvalidElementStateException'>: 
Message: invalid element state: Element is not currently interactable 
and may not be manipulated
   (Session info: chrome=55.0.2883.87)
   (Driver info: chromedriver=2.25.426924 
(649f9b868f6783ec9de71c123212b908bf3b232e),platform=Linux 
4.4.0-57-generic x86_64)

 
/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/errorhandler.py:192 
in function check_response() [raise exception_class(message, screen, 
stacktrace)]
 
/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/webdriver.py:236 
in function execute() [self.error_handler.check_response(response)]
 
/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/webelement.py:494 
in function _execute() [return self._parent.execute(command, params)]
 
/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/webelement.py:92 
in function clear() [self._execute(Command.CLEAR_ELEMENT)]
   /home/jfb/.config/libreoffice/4/user/Scripts/python/funds/funds.py:29 
in function login() [username.clear()]
   /usr/lib/libreoffice/program/pythonscript.py:869 in function invoke() 
[ret = self.func( *args )]
)




More information about the Tutor mailing list