【发布时间】:2020-05-02 02:21:27
【问题描述】:
我正在尝试在 Pycharm 中开发 Libreoffice Calc python 宏。我想在 Pycharm 中运行测试。当 libreoffice 在 Pycharm 外部启动时测试工作正常,但当我尝试直接从 Pycharm 内部启动它时失败。
我尝试了 2 种不同的方式来启动 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 中运行良好,例如从测试模块调用。但是当使用 python subprocess.Popen 从 Pycharm 启动 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
当 Libreoffice 在 Pycharm 中使用 subprocess.Popen 启动时,同样的提取。根本没有长笛......
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
【问题讨论】:
标签: python pycharm libreoffice