我開始自由報辦公計算器使用以下命令:的Python 3類返回使用構造器
$ 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>
那麼在構造函數中發生了什麼?它不應該仍然在那裏嗎?我試圖避免鍋爐板代碼。
P.S.我知道它實際上並不是一個結構體,我只是稱它爲結構體。 – leeand00
您從未設置'myUno.model',所以它仍然是類的定義中的'None'。 – kindall
[此github代碼]中的'UnoObjs'類(https://github.com/silnrsi/libreoffice-linguistic-tools/blob/master/LinguisticTools/pythonpath/lingt/utils/util.py)執行此操作。 –