首页 > 解决方案 > libreoffice 宏 - 在文本字段上切换启用可见

问题描述

我正在使用 python 宏对 libreoffice 编写器文件执行操作。而且我希望可以切换 TextField 的 EnableVisible 标志。

这意味着,在双击该字段时,切换您可以使用的小标志,使其可见或不可见。

到目前为止,我在我的代码中得到了这个:

import uno

def toggle_field(field_title):
    document = XSCRIPTCONTEXT.getDocument()
    textfields = document.getTextFields()
    enum = textfields.createEnumeration()
    while enum.hasMoreElements():
        tf = enum.nextElement()
        if tf.VariableName == field_title:
            visibility = tf.getPropertyValue('EnableVisible') #wrong
            tf.EnableVisible = not visibility                 #wrong
            tf.update()                                       #maybe right

这给了我那个

com.sun.star.beans.UnknownPropertyException:未知属性:启用(调用模块(...)file.py中的函数toggle_field时出错(:未知属性:EnableVisible

另外,如果我评论第一个错误的行,第二个错误的行给我

com.sun.star.beans.UnknownPropertyException:未知属性:启用(调用模块(...)file.py中的函数toggle_field时出错(:EnableVisible


更新 :

tf.IsFieldDisplayed = False

或者

tf.setPropertyValue('IsFieldDisplayed', False)

不再是未知属性,但我收到此错误消息:

com.sun.star.beans.UnknownPropertyException:IntrospectionAccessStatic_Impl::setPropertyValueByIndex(),索引 13 处的属性是只读的(调用模块 (...)file.py 中的函数 toggle_field 时出错(:IntrospectionAccessStatic_Impl::setPropertyValueByIndex(),属性位于索引 13 是只读的

看起来不公平,因为它在文档中不是只读的,BASIC 可以修改它(https://wiki.documentfoundation.org/images/b/b0/BH5009-Macros.pdf第 19 页)

标签: pythonlibreoffice

解决方案


经过共同的研究工作,事实证明该属性称为IsVisible

 tf.IsVisible = not tf.IsVisible

推荐阅读