Learn anything with flashcards

How to build an Anki deck programmatically

Filip Razek
3 min readMar 10, 2023

Anki is probably the best tool for anything you need to remember, whether you’re learning a new language, studying for your exams or teaching yourself how to code.

If you’ve used Anki for some time, you will probably agree that creating cards is what takes the most time. Today, I would like to show you how to easily build a deck programmatically — that is, without using the Anki interface too much. In this example, I’ve decided to build a deck of the 300 most frequently used words in Spanish (something you can find tons of examples online).

Getting started

Let’s create a new folder and initialize a TS node project in it:

npm init ts-node-project
npm install

Getting the data

I’ve decided to scrape a list I found on strommeninc.com. The data is nicely formatted in a table:

The table of words we would like to learn

I decided to use axios + cheerio to get the list of words. If you’re familiar with JQuery or anything web scraping-related, you shouldn’t have trouble understanding this part.

In the src folder, create a scrape.ts file:

Code for our word scraper

In the scrapeVocabulary function, we fetch the page with axios, then use cheerio to parse the table rows and the words in the table cells.

  • What does the .slice(1, limit + 1) mean? We slice the rows to eliminate the table header and allow a limit parameter to be passed (for example, you might only want the 300 most frequent words).
  • What is the Word type we used? It’s a way to represent the objects we want to work with. We’ll define it in our index.js.

Writing the data to a file

The Anki docs tell us that we can import a deck from a text file, using comma separated values (similar to a .csv file).

In our code, we simply have to write the words we just downloaded to a file. In the src directory, create a writeWordsToFile.ts file:

Write the vocabulary to a deck file

Formatting the words

To make the words easier to learn, we’ll add small flags next to them.

In addition to that, you might have noticed that the Spanish word on row 139 contained a comma, which might mess up our import (just kidding, but Anki would have told you when running your script).

Let’s write a formatWords.ts file (still in our src directory), and write our formatting functions:

Formatting our vocabulary words

Putting it all together

Finally, we have all of our “business” logic written. Let’s now create an index.ts file, which will simply link it all together.

Putting it all together in index.ts

Now, we simply run the npm start command, and poof! Our words appear in the anki.txt file. We can import them directly in the Anki desktop app (don’t forget to create a new deck!)

If you want to check out the source code for the app, it’s available online!

Thanks!

Still reading? You must be pretty good at paying attention!

I would love to hear your feedback on my other projects, so feel free to look at my GitHub ;)

Thanks for reading and happy coding!

--

--

Filip Razek
Filip Razek

Written by Filip Razek

A CentraleSupélec student living in the Czech Republic. Check out my other projects at https://github.com/FilipRazek/

No responses yet