-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathankiconvert.py
70 lines (51 loc) · 1.6 KB
/
ankiconvert.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import csv
import random
import genanki
from pathlib import Path
MODEL_ID = 1260270622
DECK_PREFIX = '' # TODO Replace with your deck prefix (ie. Biochemistry::)
PACKAGE_NAME = '' # TODO Replace with your package name (ie. Biochemistry 2102)
my_model = genanki.Model(
MODEL_ID,
'Simple Model',
fields=[
{'name': 'Question'},
{'name': 'Answer'},
],
templates=[
{
'name': 'Card 1',
'qfmt': '{{Question}}',
'afmt': '{{FrontSide}}<hr id="answer">{{Answer}}',
},
])
def main():
my_decks = []
pathlist = sorted(Path('./results/by_chapter/').glob('Chapter*.csv'))
for path in pathlist:
# because path is object not string
path_in_str = str(path)
print(path_in_str)
with open(path_in_str, 'r') as csvfile:
csv_reader = csv.reader(csvfile, delimiter=' ', quotechar='|')
chapter_title = path.stem
my_decks.append(create_chapter_deck(chapter_title, csv_reader))
if my_decks:
genanki.Package(my_decks).write_to_file(f'{PACKAGE_NAME}.apkg')
def create_chapter_deck(chapter, chapter_rows):
my_deck = genanki.Deck(
random.randrange(1 << 30, 1 << 31),
f'{DECK_PREFIX}::{chapter}')
first = True
for row in chapter_rows:
if first:
first = False
continue
my_note = genanki.Note(
model=my_model,
fields=[row[0], row[1]])
my_note.tags.append(chapter.replace(' ', '-'))
my_deck.add_note(my_note)
return my_deck
if __name__ == '__main__':
main()