Don’t thnks this is possible directly, but you migth be able to execute the python script when stored in the resources folder and try to invoke the script froma java action with the help of Apache Commons Exec for example.
Never tried this though.
For some ideas on doing this from java have a look at: https://www.baeldung.com/java-working-with-python
None of the options in the baeldung article is useful. Jython is too old, running a shell won't work because Python is not on the server.
You can run Python in a container on another server and commnicate with REST.