48

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

Nicolas Raoul
  • 10,711
  • 18
  • 64
  • 102
Thalecress
  • 1,317
  • 3
  • 16
  • 19

3 Answers3

42

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

Aaron Thoma
  • 690
  • 8
  • 16
nispio
  • 497
  • 5
  • 13
18

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:

Casey Jones
  • 110
  • 9
gavenkoa
  • 1,906
  • 5
  • 31
  • 38
  • Thanks - this example is really helpful. But what does `FBASENAME` refer to? – Casey Jones Dec 30 '20 at 09: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 '20 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.

bmaupin
  • 313
  • 3
  • 11