【问题标题】:Why does desktop.getCurrentComponent() return None in PyUNO?为什么 desktop.getCurrentComponent() 在 PyUNO 中返回 None?
【发布时间】:2020-12-19 21:36:47
【问题描述】:

尝试恢复一个名为 Wavelet 的 PyUNO 示例脚本,以了解当今 LO 的工作原理并重新启动。由于 LibreOffice 和 UNO 与脚本的创建时间有所不同,我遇到了问题。

设法获取桌面对象。现在我想检索打开文档的组件。我该如何正确实现这一目标? desktop.getCurrentComponent() 调用返回None

LibreOffice 版本:6.4.6.2.

系统:Ubuntu MATE 20.04 x86_64。

代码如下:

#!/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( () )

输出:

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/

【问题讨论】:

    标签: python libreoffice pyuno


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回答!不幸的是,它不起作用:Traceback (most recent call last):File "./wavelet.py", line 62, in &lt;module&gt;wavelet.trigger( () )File "./wavelet.py", line 24, in triggersearch = desktop.getCurrentComponent().createSearchDescriptor()AttributeError: 'NoneType' object has no attribute 'createSearchDescriptor'也许我们刚刚发现了一个错误。
    【解决方案2】:

    尝试等待文档组件可用。它奏效了:

    doc = None
    while doc is None:
        doc = desktop.getCurrentComponent()
    

    【讨论】:

      猜你喜欢
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      • 2015-06-18
      • 2016-06-20
      相关资源
      最近更新 更多