31

Can I convert my CSV file into an Anki deck? I can't find any options in the program.

26

The desktop Anki version will allow you to import "Text separated by tabs or semicolons." Use this option to choose your CSV file. After opening the file, you will be presented with a dialog which allows you to customize how your data is imported. One of the settings is an option that lets you choose the delimiter. Change this to a comma, and it should work for you.

Screenshot: Importing a CSV file into Anki

  • 3
    Also you have to set up an encoding for UTF-8 – user429484 Mar 19 '15 at 8:33
  • 1
    Can it be used to insert new cards into existing deck? – Septagram Jun 11 '16 at 11:29
  • How did you do that? I have a deck with "Note ID". Yet, it doesn't appear in the mapping choices: just Front, Back or Ignore. – Kaz Dec 5 '17 at 8:48
8

Another way to generate .apkg file programmatically by reusing desktop version with Python. Extend:

PYTHONPATH=/usr/share/anki: python ...

and run script (of course you should adapt it to your needs):

import anki
from anki.exporting import AnkiPackageExporter

collection = anki.Collection(os.path.join(TMPDIR, 'collection.anki2'))

deck_id = collection.decks.id(FBASENAME + "_deck")
deck = collection.decks.get(deck_id)

model = collection.models.new(FBASENAME + "_model")
model['tags'].append(FBASENAME + "_tag")
model['did'] = deck_id
model['css'] = """
.card {
  font-family: arial;
  font-size: 20px;
  text-align: center;
  color: black;
  background-color: white;
}
.from {
  font-style: italic;
}
"""

collection.models.addField(model, collection.models.newField('en'))
collection.models.addField(model, collection.models.newField('ru'))

tmpl = collection.models.newTemplate('en -> ru')
tmpl['qfmt'] = '<div class="from">{{en}}</div>'
tmpl['afmt'] = '{{FrontSide}}\n\n<hr id=answer>\n\n{{ru}}'
collection.models.addTemplate(model, tmpl)
tmpl = collection.models.newTemplate('ru -> en')
tmpl['qfmt'] = '{{ru}}'
tmpl['afmt'] = '{{FrontSide}}\n\n<hr id=answer>\n\n<div class="from">{{en}}</div>'
collection.models.addTemplate(model, tmpl)

model['id'] = 12345678  # essential for upgrade detection
collection.models.update(model)
collection.models.setCurrent(model)
collection.models.save(model)

note = anki.notes.Note(collection, model)
note['en'] = "hello"
note['ru'] = u"[heləʊ]\nint. привет"
note.guid = "xxx1"
collection.addNote(note)

note = collection.newNote()
note['en'] = "bye"
note['ru'] = u"[baɪ]\nint. пока"
note.guid = "xxx2"
collection.addNote(note)

export = AnkiPackageExporter(collection)
export.exportInto(FONAME)

As long you keep note.guid and model['id'] same you can import DB with updating cards without losing progress!

My production code examples:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.