I'm working on a python script that call's OO or LO Calc and needs to get all the named ranges from the spreadsheet.
So far the code below can to launch OO, attach to the current spreadsheet and read write to individual cells. The next step is to read all the named ranges.
I saw some macro examples, along the lines of
h = ThisComponent
for i in h.namedRanges:
print(i)
However in an external python script "h = ThisComponent" does not work, how do I perform this action in my code?
Or if anyone can point me to some documentation for embedding OO/LO into Python app.
Code: Select all
import sys
sys.path.append('/usr/lib/libreoffice/program')
import uno
import subprocess
run_soffice = [
'soffice',
'--accept=socket,host=localhost,port=2002;urp;StarOffice.Service',
'--nologo',
'--norestore',
]
subprocess.Popen(run_soffice)
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext)
ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop",ctx)
model = desktop.getCurrentComponent()
active_sheet = model.CurrentController.ActiveSheet
cell1 = active_sheet.getCellRangeByName("C4")
cell1.String = "Hey , I'm working here!"
Thanks for the help.