56 lines
1.4 KiB
Python
56 lines
1.4 KiB
Python
import json
|
|
import genanki
|
|
import sys
|
|
|
|
# Create a new model for our cards. This is necessary for genanki.
|
|
MY_MODEL = genanki.Model(
|
|
1607372319,
|
|
"Simple Model",
|
|
fields=[
|
|
{"name": "Title"},
|
|
{"name": "Question"},
|
|
{"name": "Answer"},
|
|
],
|
|
templates=[
|
|
{
|
|
"name": "{{Title}}",
|
|
"qfmt": "{{Question}}",
|
|
"afmt": "{{FrontSide}}<hr id='answer'>{{Answer}}",
|
|
},
|
|
])
|
|
|
|
def json_file_to_package(json_path):
|
|
with open(json_path, 'r', encoding='utf-8') as f:
|
|
json_data = json.load(f)
|
|
package = to_package(json_data)
|
|
|
|
return package
|
|
|
|
def to_package(deck_json):
|
|
deck_title = deck_json["DeckTitle"]
|
|
deck = genanki.Deck(1607372319, deck_title)
|
|
|
|
for card_json in deck_json["Cards"]:
|
|
title = card_json["Title"]
|
|
question = card_json["Question"]
|
|
answer = card_json["Answer"]
|
|
|
|
note = genanki.Note(
|
|
model=MY_MODEL,
|
|
fields=[title, question, answer]
|
|
)
|
|
|
|
deck.add_note(note)
|
|
|
|
return genanki.Package(deck)
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) != 3:
|
|
print("Usage: python convert.py <input_json> <output_apkg>")
|
|
sys.exit(1)
|
|
|
|
input_json = sys.argv[1]
|
|
output_apkg = sys.argv[2]
|
|
json_file_to_package(input_json).write_to_file(output_apkg)
|
|
print(f"Deck created at: {output_apkg}")
|