Skip to content

Generate Anki flashcards with Python

Create and activate a virtual environment, then install the public PyPI package:

Terminal window
python -m venv .venv
. .venv/bin/activate
python -m pip install ankiforge==0.3.1

On Windows, activate with .venv\Scripts\Activate.ps1. Use ordinary CPython 3.11 or later and a matching platform wheel; CPython 3.11–3.14 is covered by recorded release checks; see verified environments.

Save this as main.py:

from ankiforge import Project, Note, BuildOptions
project = Project('spanish', default_deck='Spanish')
project.add('es:hola', Note.basic('hola', 'hello'))
output = project.build(BuildOptions.to('spanish.apkg'))
print(output.artifact.path)
Terminal window
python main.py

Open the persistent spanish.apkg in Anki. It contains hola → hello in Spanish. Keep namespace spanish and note key es:hola stable for later edits. Exporting does not require Anki to be installed.

For a vocabulary spreadsheet, follow CSV to Anki. To draft cards from source notes, start with AI-assisted card generation.

For a complete media and update task, follow JSON to Anki.

For native source builds, use source builds.

View Markdown