The output above suggests you are trying to run your script from within LibO. That wouldn’t be all that helpful, I think, because I believe your REPL would likely be hidden to you. My answer to the other question perhaps erroneously assumed you were connecting to LibO in a “remote context”. Let me explain how to do that, in case it’s helpful.
Meanwhile, the answer to this question is “don’t embed IPython within a script that you are executing from within LibreOffice”.
Now, to get you started in case you do want to utilize LibO via a remote connection… I do not know what operating system you are running, but the below set of commands assumes Ubuntu. Note that $
is the prompt, and should not be typed by you:
# From the raw command line, e.g., gnome-terminal, konsole, xterm
$ sudo apt-get update # ensure you've the latest software indexes
$ apt-cache search ipython # see what packages with 'ipython' are available
$ sudo apt-get install ipython # install the ipython package
The above just makes sure you’ve IPython available. The next starts LibO so that you can remotely connect to it:
$ libreoffice "--accept=socket,host=localhost,port=2002;urp;"
You can change that number to anything you like between 1025 and 65535 (assuming that port is not already in use by some other program), but 2002 seems to be the de facto LibO/OO choice. The next step is to get your Python script to connect to LibO on that port. Below is some boiler plate code to accomplish this:
#!/usr/bin/env python
from sys import stderr as SE # 'SE' because I'm a lazy git.
from IPython import embed as II # 'II' because it's a personal script, and I'm lazy.
try:
# The order of these imports apparently matters. Who would have guessed?
# Not me ...
import pyuno
import uno
except ImportError:
msg = ('Python bindings to pyuno or uno not found. Try setting the '
'PYTHONPATH environment variable before running this script. Like:\n'
' export PYTHONPATH=/usr/lib/libreoffice/program\n' )
SE.write( msg )
raise
SE.write( 'Connecting to Libreoffice ... '); SE.flush()
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext(
'com.sun.star.bridge.UnoUrlResolver', localContext )
connection_url = 'uno:socket,host=localhost,port=2002;urp;StarOffice.ServiceManager'
# note that localhost means literally "this computer". You could just as
# easily connect to any IP address with a waiting LibreOffice:
#connection_url = 'uno:socket,host=10.0.0.103,port=2002;urp;StarOffice.ServiceManager'
try:
smgr = resolver.resolve( connection_url )
except:
msg = ('\nUnable to connect to LibreOffice on port 2002. Either start LO'
' with\n'
'\n $ libreoffice "--accept=socket,host=localhost,port=2002;urp;"\n'
"\nor update this script's connection string with the correct port.\n")
SE.write( msg )
raise
remoteContext = smgr.getPropertyValue( 'DefaultContext' )
desktop = smgr.createInstanceWithContext( 'com.sun.star.frame.Desktop', remoteContext)
SE.write( 'Success.'); SE.flush()
# Now, start your REPL (read-evaluate-print-loop) while developing:
SE.write( 'Starting the IPython REPL:\n\n' ); SE.flush()
II()
SE.write( '\n\nREPL exit. Continuing to execute the rest of the script ...\n\n' )
SE.write( 'Opening new Writer document ...' ); SE.flush()
url = 'private:factory/swriter'
doc = desktop.loadComponentFromURL( url, '_blank', 0, () )
SE.write( 'Completed. Empty Writer window should now be visible.' ); SE.flush()
II() # now, experiment with the doc variable. For example: doc.[tab][tab]
Now you’re off to the races. Let’s execute this script (say we called it connect_to_lib.py
:
$ python connect_to_libo.py
Good luck!