Awesome, ty, I had to do some digging in the source code, but I figured out how to use the anki Python library! It’s working perfectly!
For whatever reason, I wasn’t able to find how to do this when searching the internet, or asking the LLM-- which lead me to dipping into the SQLite DB. Articles were wanting to steer me to solutions for problems I didn’t have, like using genanki to create offline decks. >:[
Anyway, here’s my script to update live Anki notes programmatically from python! It’s really fun to use from a Jupyter Notebook as well. And maybe snippet will help some poor soul if they try Googling “Manipulate Anki data from Python”.
Use Python to update live Anki data
import anki
import anki.collection
from pathlib import Path
# open anki collection (the app must be closed!!)
col = anki.collection.Collection(Path("~/Library/Application Support/Anki2/default/collection.anki2").expanduser().as_posix())
# get the notes & decks of interest
deck_id = 1694176094392
notes = [col.get_note(x) for x in col.find_notes(f"did:{deck_id}")]
# do some cool updates
for note in notes:
note["Spanish"] = note["Spanish"].strip() # i.e. remove leading/trailing whitespace!
# commit updates
col.update_notes(notes)
print(f"Updated {len(notes)} notes.")
# close, and reopen Anki app
col.close()
Just make sure to backup in the Anki app: Anki > File > Create Backup!!!