48

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

0

3 Answers 3

43

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
  • 3
    Also you have to set up an encoding for UTF-8
    – user429484
    Mar 19, 2015 at 8:33
  • 1
    Can it be used to insert new cards into existing deck?
    – Septagram
    Jun 11, 2016 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, 2017 at 8:48
20

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!

examples of my production code:

2
  • Thanks - this example is really helpful. But what does FBASENAME refer to? Dec 30, 2020 at 9:44
  • You have to provide some unique names, later you'll see them in card browser. Choose whatever you want, for consistency I externalized it at the prefix, plz follow links to actual code in the repository for a working example.
    – gavenkoa
    Dec 30, 2020 at 17:27
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .