我尝试在Pycharm中开发Libreoffice Calc python宏。我想在Pycharm内部运行测试。当在Pycharm外部启动Libreoffice时,测试工作正常,但当我尝试直接从Pycharm内部启动它时,测试失败。
我尝试了两种不同的方法来启动libreoffice。
从我的Ubuntu终端
/opt/libreoffice6.4/program/soffice --calc --accept="pipe,name=lopipe;urp;StarOffice.ServiceManager" --nologo&
使用python函数:
args = ["/opt/libreoffice6.4/program/soffice", "--calc",
"--accept=\"pipe,name=lopipe;urp;StarOffice.ServiceManager\""]
subprocess.Popen(args, universal_newlines=True)
下面的代码提供了活动工作表。
def current_active_sheet():
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", localContext)
ctx = resolver.resolve("uno:pipe,name=lopipe;urp;StarOffice.ComponentContext")
desktop = ctx.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)
model = desktop.getCurrentComponent()
return model.CurrentController.ActiveSheet
当从ubuntu终端启动Libreoffice时,current_active_sheet
函数在Pycharm中运行得很好,例如从测试模块中调用。但是当从Pycharm用python subprocess.Popen
启动Libreoffice时,该函数会引发连接异常,尽管libreoffice运行正常。
我已经尝试了这两个套接字和管道选项的uno桥,并得到相同的行为。lsof
跟踪给予了一些奇怪的东西,好像pycharm没有将管道名称传送到系统。
下面是从ubuntu终端启动libreoffice时lsof
结果的摘录,其中引用了lopipe
。
soffice.b 13506 13517 yves 7u unix 0x0000000000000000 0t0 336561 /tmp/OSL_PIPE_1000_SingleOfficeIPC_6e6e1dcdf8a8f89eb21d8fe15230aa95 type=STREAM
soffice.b 13506 13517 yves 54u unix 0x0000000000000000 0t0 310587 /tmp/OSL_PIPE_1000_lopipe type=STREAM
当使用subprocess.Popen
从Pycharm中启动Libreoffice时,也会出现同样的摘录。
soffice.b 13407 13418 yves 7u unix 0x0000000000000000 0t0 307196 /tmp/OSL_PIPE_1000_SingleOfficeIPC_6e6e1dcdf8a8f89eb21d8fe15230aa95 type=STREAM
soffice.b 13407 13419 yves 7u unix 0x0000000000000000 0t0 307196 /tmp/OSL_PIPE_1000_SingleOfficeIPC_6e6e1dcdf8a8f89eb21d8fe15230aa95 type=STREAM
2条答案
按热度按时间ubby3x7f1#
如果在一个子进程中启动,包括一个时间延迟,如
time.sleep()
,这样LO在执行API代码之前有几秒钟的时间启动。当从终端启动时,这是不需要的,因为顺序启动允许LO有时间启动。通常我会建议从终端启动LO,尽管它应该有可能与一个子进程一起启动。此外,我已经有了更好的成功连接与套接字,而不是与管道。不确定PyCharm具体;我以前用过UNO API,但没有用过子进程。
最后,试试下面的链接。这两个都需要大量的阅读,但似乎有很好的信息。
您尝试用PyCharm做的事情通常是用Java IDE做的,所以网上应该有各种各样的例子。
yrdbyhpb2#
这实际上是一个相当简单的错误。当你在python脚本中启动时,你不应该在
args
中包含引号。这是一个错误,你需要在windows命令行中启动它。下面是启动要与之交互的LO计算示例的正确方法:
从Ubuntu命令行:
从Windows Git bash终端:
在python脚本中:
然后,您可以在python中访问此示例:
几点补充意见:
time.sleep
是非常有效的lopipe
名称更改为唯一的名称,使用uuid
包是一个很好的解决方案