Search code examples
pythonlibreoffice

libreoffice macro - toggle enablevisible on a textfield


I'm using python macro to do things to a libreoffice writer file. And I'd like a possibility to toggle the EnableVisible flag of a TextField.

That means, toggle the little flag that you can use, when double clicking on that field, to make it visible or invisible.

So far I got this in my code :

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

This give me that

com.sun.star.beans.UnknownPropertyException: Unknown property: Enabled (Error during invoking function toggle_field in module (...)file.py (: Unknown property: EnableVisible

Also, if i comment te first wrong line, the second wrong line give me

com.sun.star.beans.UnknownPropertyException: Unknown property: Enabled (Error during invoking function toggle_field in module (...)file.py (: EnableVisible


update :

tf.IsFieldDisplayed = False

or

tf.setPropertyValue('IsFieldDisplayed', False)

is no longer an unknown property, but i got this error message :

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

what seems unfair, because it is not readonly in the doc, and BASIC can modify it (https://wiki.documentfoundation.org/images/b/b0/BH5009-Macros.pdf page 19)


Solution

  • After a common research effort, it turns out that the property is called IsVisible:

     tf.IsVisible = not tf.IsVisible