Automatically find and then move a audio file to another field

I recently downloaded a shared deck which has a field called ‘Pronunciation’ with IPA phonetics and a recording in the same field - I want to move the audio file to another field without having to manually do it for all 5000 words is there a way to ‘Find’ and then ‘move’ the audio file??

Maybe try using this code in the debug console.
Replace NoteTypeName with the actual name of the note type (e.g. Basic) and TargetFieldName with the name of the field you want to move the audio to.

It is a good idea to do a backup before running the code

theModel = mw.col.models.by_name('NoteTypeName')
theNotes = mw.col.models.nids(theModel)
s = r'\[sound:.*\]'
for nid in theNotes:
  note = mw.col.get_note(nid)
  m = re.search(s, note['Pronunciation'])
  if (m): 
    note['TargetFieldName'] = m.group(0)
    note['Pronunciation'] = re.sub(s, '', note['Pronunciation'])
    note.flush()
1 Like

Thank you this worked

I get an error in Anki Desktop for Windows version 23.10.1 (fac9e0ee)⁩:

File “”, line 6, in
NameError: name ‘re’ is not defined

How can I fix this?

Try prepending import re.

For example, this seems to work on my computer (using Anki 23.12.1):

import re

theModel = mw.col.models.by_name('NoteTypeName')
theNotes = mw.col.models.nids(theModel)
s = r'\[sound:.*\]'
for nid in theNotes:
  note = mw.col.get_note(nid)
  m = re.search(s, note['SourceFieldName'])
  if (m): 
    note['TargetFieldName'] = m.group(0)
    note['SourceFieldName'] = re.sub(s, '', note['SourceFieldName'])
    note.flush()

That fixed it, thanks! I got a few warnings like the following, but that’s not a problem to do. The Python my Anki’s using is “Python 3.9.15 Qt 6.6.0 PyQt 6.6.0”, btw.

<string>:11:flush() is deprecated: please use col.update_note()