Skip to content

Instantly share code, notes, and snippets.

@alsanchez
Last active November 3, 2015 23:34
Show Gist options
  • Save alsanchez/e6b59d786b9ad1c792eb to your computer and use it in GitHub Desktop.
Save alsanchez/e6b59d786b9ad1c792eb to your computer and use it in GitHub Desktop.
import re
from anki import Collection
from anki.media import MediaManager
regex = re.compile(u"\[sound:[^\]]+]")
col = Collection("<collection-path>.anki2")
mm = MediaManager(col, None)
ids = col.findCards("deck:<deck-name>")
for id in ids:
card = col.getCard(id)
note = card.note()
hanzi = note.fields[1]
filepath = u"zh/{}.mp3".format(hanzi)
filepath = mm.addFile(filepath)
note.fields[2] = re.sub(regex, "", note.fields[2])
note.fields[2] += "[sound:" + filepath + "]"
note.flush()
mm.close()
col.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment