-
Notifications
You must be signed in to change notification settings - Fork 0
/
anki_deck_generator.py
36 lines (33 loc) · 1.11 KB
/
anki_deck_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import genanki
class AnkiDeckGenerator:
def __init__(self, deck_name: str):
self.deck_name = deck_name
self.deck = genanki.Deck(2059400110, self.deck_name)
self.model = genanki.Model(
1607392319,
'Simple Model',
fields=[
{'name': 'Question'},
{'name': 'Answer'},
],
templates=[
{
'name': 'Card 1',
'qfmt': '{{Question}}',
'afmt': '{{FrontSide}}<hr id="answer">{{Answer}}',
},
])
def add_card(self, front: str, back: str):
"""
Add a card to the Anki deck with the given front and back.
"""
note = genanki.Note(
model=self.model,
fields=[front, back])
self.deck.add_note(note)
def generate_deck(self) -> str:
"""
Generate the Anki deck and return the file name.
"""
genanki.Package(self.deck).write_to_file(f'{self.deck_name}.apkg')
return f'{self.deck_name}.apkg'