Connect Anki with python

Hello,
I’m writing this topic on the forum because I have a problem that I haven’t found a solution to. I currently have a deck with a lot of cards translating words from French to English (these are two-way generalities) but I would like to modify them. Indeed, I would like to be able to add the phonetics next to each of my words. I have found a python module that does exactly what I want. The problem is that I haven’t found any way to modify a deck of cards with python while keeping the current learning of the cards (not changing their dates). Does anyone have a solution to this problem?

NB: if you do not have a unique identifier in your left-most column (i.e. the first field on the Note), then you will need to do that first, before you follow any of the steps below. Use this add-on, which will help you set up the unique ID and then go from there.

  1. Save a copy of your deck by exporting it and ensure the option to include Scheduling Information is ticked.
  2. Try exporting the notes to a spreadsheet (e.g. LibreOffice Calc). Add the column or columns that you require for the new information.
  3. Update the Note Type so that it has a field to match the column that you added. Add more fields, if you added more than one column. Ensure that you arrange the columns in the same order as that found in the spreadsheet.
  4. Save the spreadsheet as a CSV.
  5. Import the spreadsheet into your deck. It will match on the first column and update the data, but not change any of the scheduling information.

I haven’t done this myself, yet, but I think it should be possible to import the module via importlib.util in Anki’s debug console.Then you can use Anki’s API to iterate over the relevant notes directly and insert phonetics using the imported module.

If anyone has already done something similar, I’d also be interested to know.

1 Like

I think it might be easier to add the module parent directory to sys.path, and use a plain import statement.

1 Like

I have tried opening the file with LibreOffice calc, notepad and even DB Browser to read my deck but it seems to struggle to read the cards. At the beginning of the document, it’s sql code but as soon as I go down, it becomes unreadable (ex: “x�V8w��ka1�i”)

As for the console, I can’t launch it, I don’t really understand the steps to take.

What about using FooSoft Productions - AnkiConnect ?

1 Like

https://docs.ankiweb.net/misc.html

Also, I may be wrong, but I don’t think you can open mysql database file inside spreadsheet softwares. You’ll want to export the notes to csv first, or use a database viewer softwares instead.

It seems to me to be the ideal solution

I think using AnkiConnect would be your best bet, but if you understand this section of the Anki add-on writing guide, I think you can also easily achieve your goal by using importlib and Anki’s debug console as @Rumo said.

Here is an example of how to do it:

Library:

Deck

  • Deck name: phonetic test

Note type

  • Fields
    • English
      • e.g. apple, ball, cat, dog, egg
    • IPA
    • Back

Debug console

  • Start Anki and open the debug console.
  • Enter the following code at the upper part of the console, then run it with Ctrl + Enter ( Command + Return on a Mac):
import importlib.util
import sys

# https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
lib_path = r"C:\tmp-py-lib\eng_to_ipa\__init__.py"
spec = importlib.util.spec_from_file_location("eng_to_ipa", lib_path)
module = importlib.util.module_from_spec(spec)
sys.modules["eng_to_ipa"] = module
spec.loader.exec_module(module)

import eng_to_ipa as ipa


updated_notes = []

# Iterate over all notes in a deck
for note_id in mw.col.find_notes('"deck:phonetic test"'):
    note = mw.col.get_note(note_id)
    if not note["IPA"]:
        note["IPA"] = ipa.convert(note["English"])
        updated_notes.append(note)

if updated_notes:
    mw.col.update_notes(updated_notes)

Screencast (Gif):

eng-ipa

2 Likes

Julien,

export the deck (from the Anki home screen, click on the cog (la roue dentée) to the right of the deck and choose Export).

For Export format, choose Notes in Plain Text and ensure the two options are selected (Include Tags, and Include HTML and media references).

The resultant file (ending in .txt) should be opened by LibreOffice Calc. When asked how to interpret it, you should select Tab (tabulation) as the character that separates columns of data.

Now you will see all your data correctly. I’d suggest that you follow the steps I mentioned above — it would be better to have the phonetic transcription as a separate field (Anki; or column in Excel).

1 Like

In fact, I’ve already tried this way, it’s ideal because it’s very simple to use a calc in a program but if I do that, I lose all the intervals of the cards.

If you re-import the data from a CSV, as I mentioned above, you will not lose any of your intervals. You will only lose them, if you change the first field in your spreadsheet.

Anki keeps the first field of a Note as its Unique Identifier. It checks that upon import; if it finds a match, it just updates what has changed.

Have you changed your first column?

Do you need a hand? I could connect via Zoom and talk you through it, if you like?

1 Like

I was tweaking the program to modify the calc but I must admit that the solution given by hkr is more than perfect. I didn’t know you could do so much with the anki console.
It’s very nice to offer such help matta but now that the program is working I don’t need any help.
Thank you all for being so helpful, I think it’s time to close this topic.
The anki community is really nice

3 Likes