Skip to content
Rubaiyat Khondaker edited this page Dec 6, 2020 · 14 revisions

Obsidian_to_Anki

Script to add flashcards from a text or markdown file to Anki. Run from the command line. Built with Obsidian markdown syntax in mind. Supports user-defined custom syntax for flashcards.
See the Trello for planned features.

Features

Current features:

Note that all custom syntax is off by default, and must be programmed into the script via the config file - see Custom syntax for instructions.

Who is this for?

It might be useful to show the motivation for me personally writing the script in the first place.
My workflow is essentially to have notes be flashcards - one of my notes (before I discovered Obsidian). However, it got tedious to keep copy-pasting cards into Anki, so I got the idea to write a script to do it automatically.

However, you don't need to have your notes be files of flashcards to use this script! You just need to be fine with visibly embedding flashcards in your notes, and keeping them there for future reference/editing. The script will ignore anything it doesn't think is a flashcard, so you're free to add context/information not needed for Anki to your notes.

Setup

  1. Install the latest version of Python.
  2. Start up Anki, and navigate to your desired profile.
  3. Ensure that you've installed AnkiConnect.
  4. If you are a new user, download obstoanki_setup.py from the releases page, and place it in the folder you want the script installed (for example your notes folder).
  5. Run obstoanki_setup.py, for example by double-clicking it in a file explorer. This will download the latest version of the script and required dependencies automatically. Existing users should be able to run their existing obstoanki_setup.py to get the latest version of the script.
  6. Check the Permissions tab below to ensure the script is able to run.
  7. Run obsidian_to_anki.py, for example by double-clicking it in a file explorer. This will generate a config file, obsidian_to_anki_config.ini.

See Troubleshooting if you have problems.

Permissions

The script needs to be able to:

  • Make a config file in the directory the script is installed.
  • Read the file in the directory the script is used.
  • Make a backup file in the directory the script is used.
  • Rename files in the directory the script is used.
  • Remove a backup file in the directory the script is used.
  • Change the current working directory temporarily (so that local image paths are resolved correctly).

Usage

Apart from editing the config file, all operations of the script require Anki to be running.
New in v2.7.0 - if you supply Anki Path and Anki Profile, the script will automatically attempt to open Anki if it isn't already running.

The GUI of the script looks like this:
GUI

Hopefully the options and path are self-explanatory. The 'recurse' option can be used on the top-level notes directory - it'll pick up all the notes in subfolders automatically. Note that you can run the script over the same file twice perfectly fine - it won't add duplicate cards.

Command line usage

If you set 'GUI' in the config file to False, the script is then run from the command line:

  • Use -h to see help.
  • Run the script as obsidian_to_anki.py [path], where [path] is the path to the file or folder you wish to add notes from.
  • Use -c to open up the config file for editing (not guaranteed to work on all operating systems, if it doesn't you'll have to find and edit it manually).
  • Use -u to update the config file. Do this when you add new note types to Anki.
  • Use -m to force the script to add all media files detected, instead of lazy addition of media files. Useful if you've e.g. resized the image, and want the changes to be reflected in Anki.
  • Use -r to use custom regex syntax, ignoring the default syntax of the script.
  • Use -R to recursively scan subfolders.

New users

If you are a new user, these steps are recommended:

  1. Check Custom syntax to see if there is a template that works for you.
  2. Then, check the information on the following topics:
  1. You should be good to go simply running the script with the 'Regex' option checked.

The sections below describe the default syntax of the script (with the 'Regex' option not checked).

Deck formatting

Note formatting

Deleting notes

Inline notes

Cloze formatting

By default, the script adds to the current profile in Anki.

Troubleshooting

Technical

Clone this wiki locally