2010-01-28 55 views
6

我試圖創建一個python程序(使用pyUNO)在OpenOffice calc頁面上進行一些更改。使用外部Python程序在OpenOffice上加載文檔

我以前在「接受」模式下啓動了OpenOffice,可以從外部程序進行連接。顯然,應該是一樣簡單:

import uno 
# 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) 

#The calling it's not exactly this way, just to simplify the code 
DESKTOP.loadComponentFromURL('file.ods') 

但我得到一個AttributeError當我嘗試訪問loadComponentFromURL。如果我做一個dir(DESKTOP),我已經看到的只有以下屬性/方法:

['ActiveFrame', 'DispatchRecorderSupplier', 'ImplementationId', 'ImplementationName', 
'IsPlugged', 'PropertySetInfo', 'SupportedServiceNames', 'SuspendQuickstartVeto', 
'Title', 'Types', 'addEventListener', 'addPropertyChangeListener', 
'addVetoableChangeListener', 'dispose', 'disposing', 'getImplementationId', 
'getImplementationName', 'getPropertySetInfo', 'getPropertyValue', 
'getSupportedServiceNames', 'getTypes', 'handle', 'queryInterface', 
'removeEventListener', 'removePropertyChangeListener', 'removeVetoableChangeListener', 
'setPropertyValue', 'supportsService'] 

我讀過,有些情況下做同樣的錯誤,但OpenOffice的3.0(我使用的OpenOffice 3.1以上紅帽子5.3)。我試圖使用here這個解決方法,但他們似乎沒有工作。

任何想法?

回答

4

以來,它一直我做了什麼PyUNO很長一段時間,但看着那工作,我跑了回來06年最後一次的代碼,我做我的負荷文件是這樣的:

def urlify(path): 
    return uno.systemPathToFileUrl(os.path.realpath(path)) 

desktop.loadComponentFromURL(
     urlify(tempfilename), "_blank", 0,()) 

你示例是一個簡化版本,我不確定是否有意或無意刪除了額外的參數。

如果loadComponentFromURL不存在,那麼API已經改變了,或者還有其他的錯誤,我已經讀過你的代碼,它看起來像你正在做我所有的相同的事情。

我不相信桌面對象上的方法的dir()會很有用,因爲我認爲有一個__getattr__方法用於通過請求進行代理,並且您打印的所有方法都是用於com.sun.star.frame.Desktop的替代對象的實用方法。

我想也許失敗可能是沒有名爲loadComponentFromURL的方法只有1個參數。也許給出4個參數版本會導致找到並使用該方法。這可能僅僅是Python和Java之間的阻抗不匹配問題,其中Java具有呼叫簽名方法重載。

+0

的方法看是不是發現,因爲我一直在試圖獲得方法itsef,稱不從交互式shell參數: - (我也試着用四個參數來調用它,我有意地簡化它。 – Khelben 2010-02-02 09:55:48