Using UNO from a Jupyter Notebook on Ubuntu

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
babazaroni
Posts: 3
Joined: Mon May 29, 2023 2:49 am

Using UNO from a Jupyter Notebook on Ubuntu

Post by babazaroni »

Hello, I am trying to use UNO to communicate to OpenOffice from a Jupyter Notebook on Ubuntu. I am using a conda virtual environment, which means I have easy access to conda and pip installed packages. However, it appears I can only get Python3-Uno from apt and the notebook will not import it. I did try adding a PYTHONPATH environment variable with the path to where apt-get installed uno.py. However, jupyter crashed while importing it.

Is there any resources or info about how to use Uno from Jupyter Lab?

--Thanks
Openoffice 4.1 Ubuntu
User avatar
karolus
Volunteer
Posts: 1158
Joined: Sat Jul 02, 2011 9:47 am

Re: Using UNO from a Jupyter Notebook on Ubuntu

Post by karolus »

Hallo
Do you really talk about OpenOffice ? … NO CHANCE !

Code: Select all

sudo apt-get install libreoffice libreoffice-script-provider-python

Code: Select all

pre:
# modify the registrymodifications.xcu with inserting th following node (without single-quotes and linebreaks):
'<item oor:path="/org.openoffice.Setup/Office">'
'<prop oor:name="ooSetupConnectionURL" oor:op="fuse">'
'<value>pipe,name=apracadapra;urp;StarOffice.ServiceManager"</value></prop></item>')

OR (alternativly) start soffice from notebook like:

Code: Select all

from subprocess import Popen

PIPENAME = 'apracadapra' # CHANGE ME !!! 

officepath = 'soffice'
calc = '--calc'
pipe = f"--accept=pipe,name={PIPENAME};urp;StarOffice.ComponentContext"

Popen([officepath,
       calc,
       pipe]);
print(f'LibreOffice has started and is available via PIPE: {PIPENAME}') 
with one of the above ( I prefer the onetime modification of the registrymodifications.xcu ) you need in the notebook only:

Code: Select all

import uno
from pythonscript import ScriptContext

PIPENAME = 'apracadapra'  # CHANGE IT 

local = uno.getComponentContext()
resolver = local.ServiceManager.createInstance("com.sun.star.bridge.UnoUrlResolver")

ctx = resolver.resolve( "uno:pipe,"
                           f"name={PIPENAME};"
                           "urp;"
                           "StarOffice.ComponentContext")

createUnoService = ctx.ServiceManager.createInstance

(
 file_access,
 pathsubstitution,
 mri) = map(   createUnoService,(
                 "com.sun.star.ucb.SimpleFileAccess",
                 "com.sun.star.util.PathSubstitution",
                 "mytools.Mri" )
             )

XSCRIPTCONTEXT = ScriptContext(ctx, None, None)
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
babazaroni
Posts: 3
Joined: Mon May 29, 2023 2:49 am

Re: Using UNO from a Jupyter Notebook on Ubuntu

Post by babazaroni »

Thanks karulus, lots of good info there that I'm going through. And I will be using libreoffice. But I don't see where you are installing uno? Is it in libreoffice-script-provider-python?
Openoffice 4.1 Ubuntu
User avatar
karolus
Volunteer
Posts: 1158
Joined: Sat Jul 02, 2011 9:47 am

Re: Using UNO from a Jupyter Notebook on Ubuntu

Post by karolus »

babazaroni wrote: Mon May 29, 2023 8:19 pm Thanks karulus, lots of good info there that I'm going through. And I will be using libreoffice. But I don't see where you are installing uno? Is it in libreoffice-script-provider-python?
uno.py comes with libreoffice itself.

libreoffice-script-provider-python == pythonscript.py, which is responsible for the libreoffice-gui for python.

ps. IMHO there is no need to setup a virtual environment.
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
babazaroni
Posts: 3
Joined: Mon May 29, 2023 2:49 am

Re: Using UNO from a Jupyter Notebook on Ubuntu

Post by babazaroni »

Hello karolus, after battling with my environment I can now load the modules. I was able to open libreoffice.

However, in the third cell there appears to be a typo. The line 'createUnoService = ctx.ServiceManager.createInstance' I think should be a function call with the open parens down a couple lines. When I move the parens up to the end of createInstance, I get the error 'cannot assign to function call'.

Can you check the validity of that cell?
Openoffice 4.1 Ubuntu
User avatar
karolus
Volunteer
Posts: 1158
Joined: Sat Jul 02, 2011 9:47 am

Re: Using UNO from a Jupyter Notebook on Ubuntu

Post by karolus »

babazaroni wrote: Wed May 31, 2023 6:02 am Hello karolus, after battling with my environment I can now load the modules. I was able to open libreoffice.

However, in the third cell there appears to be a typo. The line 'createUnoService = ctx.ServiceManager.createInstance' I think should be a function call with the open parens down a couple lines. When I move the parens up to the end of createInstance, I get the error 'cannot assign to function call'.

Can you check the validity of that cell?
No, that's not a mistake, that's exactly how it's meant to be!
( "createUnoService" is a BASIC runtime function with the same functionality, so I don't have to translate anything from python to BASIC and vice versa when I use it later. )
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
Post Reply