I used Automator in a fashion documented here: https://superuser.com/questions/1160101/run-a-python-script-with-filename-arguments-as-a-macos-folder-action
Open Automator
Create a Quick Action and choose what you want to work on, in your case the drop-downs would be "files or folders" in "Finder".
Choose "Run Shell Script"
Choose /bin/bash or /bin/zsh or whatever shell you are comfortable with. Note: the drop-down also displays (for me) my python shells -- so you could have inline python code. However, I was to have a separate python file I run in multiple ways.
For the script you would have
/path/to/python /path/to/script.py "$1" if you selected a folder.
If you wanted to select a file in the folder and get the base name to the folder, you would use
/path/to/python /path/to/script.py "$(basename $1)"
$1 is the argument the Finder sends based on your selection when the Quick Action is run.