我使用以下命令启动了 Libre-Office Calc:
$ libreoffice --calc --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"
import uno
# Class so I don't have to do this crap over and over again...
class UnoStruct():
localContext = None
resolver = None
ctx = None
smgr = None
desktop = None
model = None
def __init__(self ):
print("BEGIN: constructor")
# get the uno component context from the PyUNO runtime
localContext = uno.getComponentContext()
# create the UnoUrlResolver
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext )
# connect to the running office
ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager
# get the central desktop object
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
# access the current writer document
model = desktop.getCurrentComponent()
print("END: constructor")
然后我称之为:
myUno = UnoStruct()
BEGIN: constructor
END: constructor
并尝试得到它
active_sheet = myUno.model.CurrentController.ActiveSheet
AttributeError: 'NoneType' object has no attribute 'CurrentController'
似乎model
是None
(空)
>>> active_sheet = myUno.model
>>> print( myUno.model )
None
>>> print( myUno )
<__main__.UnoStruct object at 0x7faea8e06748>
那么在构造函数中发生了什么?它不应该还在吗?我试图避免使用样板代码。