How to put cards on Anki Deck using Python

2k Views Asked by At

I have an Anki Deck here, is there a way that I could programmatically insert a card on my current sample deck right here? See the image provided

Anki Deck image sample

Is there a way to do that with python? Since it is known that Anki is written in Python I have the feeling that is could be possible.

Also I have found this python package that you can actually create a deck and a card but I was thinking how can I insert the card object or Note as they would call it on the documentation... How can it be inserted to a current deck in Anki? Here is the code

import genanki

my_model = genanki.Model(1607392319,'Simple Model',
  fields=[
    {'name': 'Question'},
    {'name': 'Answer'},
  ],
  templates=[
    {
      'name': 'Card 1',
      'qfmt': '{{Question}}',
      'afmt': '{{FrontSide}}<hr id="answer">{{Answer}}',
    },
  ])




my_note = genanki.Note(
  model=my_model,
  fields=['Capital of Argentina', 'Buenos Aires'])



my_deck = genanki.Deck(
  2059400110,
  'Country Capitals')

my_deck.add_note(my_note)

genanki.Package(my_deck).write_to_file('output.apkg')

Link to the github -> genanki

1

There are 1 best solutions below

0
On

You need to send a POST-request to your Anki Profile, the easiest way to do this is using Anki-Connect.

In your AnkiConnect Add-on Config file you find the webBindAddress & webBindPort, which you need as target for your POST-request: "http://{webBindAddress}:{webBindPort}"

Important is to use the absolute path with forward slashes to your deck. Then your POST-request should look something like this:

response = requests.post("http://xxx.x.x.x:xxxx", json={
  "action": "importPackage",
  "version": 6,
  "params": {
      "path": forward_slash_absolute_path
   }
})

Here you find more information about Anki-Connect and how to install: Anki-Connect

Note: if you search for your target "http://{webBindAddress}:{webBindPort}" in your browser you will get the needed version.