首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么desktop.getCurrentComponent()在PyUNO中不返回任何内容?

为什么desktop.getCurrentComponent()在PyUNO中不返回任何内容?
EN

Stack Overflow用户
提问于 2020-12-19 21:36:47
回答 2查看 238关注 0票数 1

试图恢复一个名为“小波”的PyUNO示例脚本,以了解LO现在是如何工作的,并重新启动。由于LibreOffice & UNO与脚本的创建时间略有不同,我遇到了一些问题。

设法获得桌面对象。现在我想检索打开的文档的组件。我怎样才能恰当地做到这一点?desktop.getCurrentComponent()调用返回None

LibreOffice版本: 6.4.6.2。

系统: Ubuntu 20.04 x86_64。

守则如下:

代码语言:javascript
复制
#!/usr/bin/python3

def TestWave(event):
    wavelet = Wavelet(XSCRIPTCONTEXT)
    wavelet.trigger( () )

import uno
import unohelper
import string
from com.sun.star.task import XJobExecutor

class Wavelet( unohelper.Base, XJobExecutor ):
    def __init__( self, ctx ):
        self.ctx = ctx

    def trigger( self, args ):
        desktop = self.ctx.ServiceManager.createInstanceWithContext(
            "com.sun.star.frame.Desktop", self.ctx )

        doc = desktop.getCurrentComponent()
        print('doc:', doc)

        #try:
        search = doc.createSearchDescriptor()
        search.SearchRegularExpression = True
        search.SearchString = "\\<(k|s|v|z|o|u|i|a) "

        found = doc.findFirst( search )
        while found:
            print("found:", found.String)
            found.String = string.replace( found.String, " ", u"\xa0" )
            found = doc.findNext( found.End, search)

        #except:
        #    pass


g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(
        Wavelet,
        "name.vojta.openoffice.Wavelet",
        ("com.sun.star.task.Job",),)

if __name__ == "__main__":
    import os
    # Start OpenOffice.org, listen for connections and open testing document
    os.system( "loffice '--accept=socket,host=localhost,port=2002;urp;' --writer ./WaveletTest.odt &" )
    # Get local context info
    localContext = uno.getComponentContext()
    resolver = localContext.ServiceManager.createInstanceWithContext(
      "com.sun.star.bridge.UnoUrlResolver", localContext )
    ctx = None
    # Wait until the OO.o starts and connection is established
    while ctx == None:
      try:
        ctx = resolver.resolve(
          "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
      except:
        pass
    # Trigger our job
    wavelet = Wavelet( ctx )
    wavelet.trigger( () )

输出:

代码语言:javascript
复制
doc: None
Traceback (most recent call last):
  File "./wavelet.py", line 62, in <module>
    wavelet.trigger( () )
  File "./wavelet.py", line 24, in trigger
    search = doc.createSearchDescriptor()
AttributeError: 'NoneType' object has no attribute 'createSearchDescriptor'

编辑1

交叉寄出地址如下:https://ask.libreoffice.org/en/question/283785/why-does-desktopgetcurrentcomponent-return-none-in-pyuno/

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-12-25 11:52:23

尝试等到文档组件可用为止。它起了作用:

代码语言:javascript
复制
doc = None
while doc is None:
    doc = desktop.getCurrentComponent()
票数 1
EN

Stack Overflow用户

发布于 2020-12-19 21:56:09

尝试不给desktop.getCurrentComponent()一个变量,所以擦除doc =。我记得我有这个问题,但不明白它为什么要这样做。我所记得的是,没有命名它使我的代码工作。这是我唯一能给你的建议。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65374700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档