我正在使用 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: Unknown property: Enabled (Error during invoking function toggle_field in module (...)file.py (: Unknown property: EnableVisible
此外,如果我评论第一个错误行,第二个错误行给我
com.sun.star.beans.UnknownPropertyException: Unknown property: Enabled (Error during invoking function toggle_field in module (...)file.py (: EnableVisible
更新:
tf.IsFieldDisplayed = False
或
tf.setPropertyValue('IsFieldDisplayed', False)
不再是未知属性,但我收到此错误消息:
com.sun.star.beans.UnknownPropertyException: IntrospectionAccessStatic_Impl::setPropertyValueByIndex(), property at index 13 is readonly (Error during invoking function toggle_field in module (...)file.py (: IntrospectionAccessStatic_Impl::setPropertyValueByIndex(), property at index 13 is readonly
这看起来不公平,因为它在文档中不是只读的,BASIC 可以修改它(https://wiki.documentfoundation.org/images/b/b0/BH5009-Macros.pdf 第 19 页)
请您参考如下方法:
经过共同研究,发现该属性名为 IsVisible:
tf.IsVisible = not tf.IsVisible
