2010-01-28 56 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