【发布时间】:2015-04-02 15:46:40
【问题描述】:
目前我用这个来调用现有的 LibreOffice 宏:
def OnLOtimestamp(self):
try:
pid= Popen(['lowriter '"'"'vnd.sun.star.script:fs2TimeStamp.py$fs2_TimeStamp?language=Python&location=user'"'"],shell=True).pid
except OSError, e:
self.notify_show("Timestamp Error",str(e))
self.ma2.SetLabel("Macro timestamp")
self.database['Time_stamp'] = self.database['Time_stamp'] + 1
关键位是 Popen 调用,其中宏名称是 fs2TimeStamp.py,函数是 fs2_TimeStamp,但这感觉像是在逃避,我宁愿通过 Uno 直接调用。 我的研究表明,我可能需要使用 MasterScriptProvider、XscriptProvider 和 XscriptInvocation,但尝试破译 Uno API 就像在糖浆中游泳。 有人有使用 Uno 在 Libreoffice 中调用现有宏的代码示例吗?
编辑:
到目前为止,答案似乎是否定的!
这是当前的游戏状态。
#!/usr/bin/python3
# -*- coding: utf-8 -*-
##
# a python script to run a libreoffice python macro externally
#
import uno
from com.sun.star.connection import NoConnectException
from com.sun.star.uno import RuntimeException
from com.sun.star.uno import Exception
from com.sun.star.lang import IllegalArgumentException
def test2(*args):
localContext = uno.getComponentContext()
localsmgr = localContext.ServiceManager
resolver = localsmgr.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext )
try:
ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
except NoConnectException as e:
print ("LibreOffice is not running or not listening on the port given - ("+e.Message+")")
return
except IllegalArgumentException as e:
print ("Invalid argument given - ( "+ e.Message+ ")")
return
except RuntimeException as e:
print ("An unknown error occurred: " + e.Message)
return
servmgr = ctx.ServiceManager
desktop = servmgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
model = desktop.getCurrentComponent()
# scriptP = model.getScriptProvider()
# print("scriptP", scriptP)
scriptx = model.getScriptProvider().getScript('vnd.sun.star.script:fs2TimeStamp.py$fs2_TimeStamp?language=Python&location=user')
print("scriptx", scriptx)
try:
scriptx.invoke("",0,0)
except IllegalArgumentException as e:
print ("The command given is invalid ( "+ e.Message+ ")")
return
except RuntimeException as e:
print("An unknown error occurred: " + e.Message)
return
except Exception as e:
print ("Script error ( "+ e.Message+ ")")
print(e)
return
except:
print("Error")
return(None)
test2()
当在 Libreoffice 中作为宏调用时,此代码运行良好,并且 scriptx 打印为:
scriptx <pythonscript.PythonScript object at 0x7fa2879c42e8>
但是,当从命令行运行时,脚本什么也不做,并且 scriptx 打印为:
scriptx pyuno object (com.sun.star.script.provider.XScript)0x1e749d8{, supportedInterfaces={com.sun.star.lang.XTypeProvider,com.sun.star.script.provider.XScript}}
所以 getScriptProvider 或 getScript 都没有提供他们需要的东西。我目前不知道缺少什么,但我从骨子里觉得我已经接近解决方案了。
谁能看到我哪里出错了?
【问题讨论】:
标签: python macros wxpython libreoffice