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

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
luofeiyu
Posts: 21
Joined: Thu Sep 14, 2017 2:11 am

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

Post by luofeiyu »

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

Code: Select all

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

    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?
LibreOffice 4.3.3.2 on Debian 8
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

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

Post by Villeroy »

Code: Select all

$ 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
luofeiyu
Posts: 21
Joined: Thu Sep 14, 2017 2:11 am

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

Post by luofeiyu »

When you start soffice with :

Code: Select all

soffice --calc --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"
soffice can communicate python-uno with socket and port:2002.

Code: Select all

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

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
LibreOffice 4.3.3.2 on Debian 8
Post Reply