Python Macro on LibreOffice Writer that get value from a form Text Field

I have a LibreOffice Form like this see the following image:

and a Python Macro that should get FieldText Value “Nome” and build a password, this is the source:

import random
import string

def genera_password(*args):
# Ottieni il valore del campo “Nome”
nome = XSCRIPTCONTEXT.getDocument().getTextFieldByName(“Nome”).Text

# Prendi il primo e l'ultimo carattere del nome e convertili in maiuscolo
primo_carattere = nome[0].upper()
ultimo_carattere = nome[-1].upper()

# Crea una lista di caratteri possibili per la password
caratteri_possibili = string.ascii_lowercase + string.digits + '@+*%?!$€^&'

# Crea una password casuale di 6 caratteri
password_casuale = ''.join(random.choice(caratteri_possibili) for i in range(6))

# Unisci il primo e l'ultimo carattere del nome convertiti in maiuscolo con la password casuale
password_generata = primo_carattere + password_casuale + ultimo_carattere

# Scrivi la password generata nel campo "Password"
XSCRIPTCONTEXT.getDocument().getTextFieldByName("Password").Text = password_generata

When I try to execute this macro i Get the following error:
<class 'AttributeError1> getTextFieldByName

Thanks a lot for any reply!

I think you need to get the Model of the control.

See input.py of OOO Development Tools (OooDev)
And:
build_form.py of LibreOffice Python UNO Examples

See Also get_named_control() method of OooDev forms.py

Note that it first gets models and then finds the control.

If the code is triggered by some push button or other form control of the same form:

def do_action(ev):
    txt = ev.Source.Model.Parent.getControl('Password').getText()