If you want to manipulate the calibre library from a script use calibredb
https://manual.calibre-ebook.com/gen...calibredb.html
If you want to use python you can run your python script with
calibre-debug
and then use calibre's python apis instead of calibredb. See the manual for documentation of the database apis.