How python-uno can manipulate libreoffice-calc opened from m

Keyboard macros or custom scripts

How python-uno can manipulate libreoffice-calc opened from m

Postby luofeiyu » Mon Aug 31, 2020 5:58 am

Python uno can communicate with libreoffice-calc via socket,the common way is to launch libreOffice-calc from the shell:

Code: Select all   Expand viewCollapse view
soffice --calc --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"


Then you can open a python shell ,`import uno` to manipulate `calc`.

Now i open an `calc` from menu:

Image
Code: Select all   Expand viewCollapse view
    ps aux |grep  [l]ibre
    debian    5806  0.0  0.1 213920  5908 ?        Sl   09:15   0:00 /usr/lib/libreoffice/program/oosplash --calc
    debian    5823  0.1  6.9 1088436 270148 ?      Sl   09:15   0:04 /usr/lib/libreoffice/program/soffice.bin --calc --splash-pipe=5


How can manipulate the `calc` with python-uno?
openoffice 4.3.3.2 on debian 8
luofeiyu
 
Posts: 11
Joined: Thu Sep 14, 2017 2:11 am

Re: How python-uno can manipulate libreoffice-calc opened fr

Postby Villeroy » Mon Aug 31, 2020 11:38 am

Code: Select all   Expand viewCollapse view
$ python3
Python 3.6.9 (default, Jul 17 2020, 12:50:27)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import uno
>>> localContext = uno.getComponentContext()
>>> resolver = localContext.ServiceManager.createInstanceWithContext(
...         "com.sun.star.bridge.UnoUrlResolver", localContext )
>>> resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
pyuno object (com.sun.star.uno.XInterface)0x1df73b8{, supportedInterfaces={com.sun.star.uno.XComponentContext,com.sun.star.container.XNameContainer,com.sun.star.lang.XTypeProvider,com.sun.star.uno.XWeak,com.sun.star.lang.XComponent}}
>>> ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
>>> smgr = ctx.ServiceManager
>>> dtp = smgr.createInstanceWithContext("com.sun.star.frame.Desktop",ctx)
>>> doc = dtp.getCurrentComponent()
>>> sh = doc.Sheets.getByIndex(0)
>>> print("enough spoon feeding")
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Villeroy
Volunteer
 
Posts: 29693
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How python-uno can manipulate libreoffice-calc opened fr

Postby luofeiyu » Tue Sep 01, 2020 2:29 am

When you start soffice with :

Code: Select all   Expand viewCollapse view
soffice --calc --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"


soffice can communicate python-uno with socket and port:2002.

Code: Select all   Expand viewCollapse view
sudo netstat -anp  |grep  soffice
tcp        0      0 127.0.0.1:2002          0.0.0.0:*               LISTEN      3115/soffice.bin   
unix  2      [ ACC ]     STREAM     LISTENING     45712    3115/soffice.bin     /tmp/OSL_PIPE_1000_SingleOfficeIPC_7fb3858dfd518f4aa054b3e7cf720
unix  3      [ ]         STREAM     CONNECTED     45702    3115/soffice.bin     
unix  3      [ ]         STREAM     CONNECTED     45716    3115/soffice.bin     


When you start soffice from menu:

Code: Select all   Expand viewCollapse view
sudo netstat -anp  |grep  soffice
unix  2      [ ACC ]     STREAM     LISTENING     49386    3200/soffice.bin     /tmp/OSL_PIPE_1000_SingleOfficeIPC_7fb3858dfd518f4aa054b3e7cf720
unix  3      [ ]         STREAM     CONNECTED     49390    3200/soffice.bin     
unix  3      [ ]         STREAM     CONNECTED     48919    3200/soffice.bin     


soffice can not communicate python-uno with socket ,the port:2002 is not opened.

Note : start my libreoffice-calc from menu, instead of `soffice -- `.

Image
openoffice 4.3.3.2 on debian 8
luofeiyu
 
Posts: 11
Joined: Thu Sep 14, 2017 2:11 am


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 4 guests