Can I create an Anki deck from a .CSV file?

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


Another way to generate .apkg files is by programmatically reusing the desktop version with Python. Extend:

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

Then you can adapt the following example 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'] the same, you can import the DB and update cards without losing progress!

  • Any way to build apkg from command line without GUI?
  • Is it possible to merge improvements and corrections to cards during apkg import without loosing progress?

examples of my production code:

  • http://hg.defun.work/gadict/file/tip/py/gadict_srs_anki.py or copy
  • http://hg.defun.work/gadict/file/tip/obsolete/exp_anki.py or copy

Anki API

To build on gavenkoa's answer, the Anki API has built-in functionality to import from CSV.

First of all, you can install the anki Python package using pip, e.g.

pip3 install anki protobuf

Here's a very basic example to import from CSV and export a deck to an Anki package (.apkg) file:

import anki
from anki.exporting import AnkiPackageExporter
from anki.importing.csvfile import TextImporter

# Create a new collection
collection = anki.Collection('/path/to/test.anki2'))

# Create a new deck in the collection (otherwise the "Default") deck will be used
deck_id = collection.decks.id('Deck name')
model = collection.models.byName('Basic')
model['did'] = deck_id
collection.models.save(model)

# Import cards from CSV into the new collection
importer = TextImporter('/path/to/test.csv'))
importer.initMapping()
importer.run()

# Save and export the collection to an Anki package (.apkg) file
collection.save()
exporter = AnkiPackageExporter(collection)
exporter.exportInto('/path/to/test.apkg'))

A more detailed example is here: https://github.com/bmaupin/flashcard-sets/blob/main/scripts/csv-to-apkg.py

The official Anki python API is documented here: https://addon-docs.ankiweb.net/getting-started.html

The official documentation isn't comprehensive, but I've also documented it a little bit myself: https://github.com/bmaupin/flashcard-sets/tree/main/scripts#anki-api

When using the API, it's also helpful to be familiar with some of the basic concepts: https://docs.ankiweb.net/getting-started.html#key-concepts

Genanki

Another option is to use this package: https://github.com/kerrickstaley/genanki

It's not officially affiliated with the Anki project but seems to have a cleaner and more well-documented API, in addition to a more liberal licence.