From 3c7ee303a485a02f4b90642eb4b6624b427b569d Mon Sep 17 00:00:00 2001 From: Benjamin Dweck Date: Thu, 7 Sep 2023 14:05:57 +0300 Subject: [PATCH] Initial commit --- .gitignore | 4 ++++ csv2ankicards.py | 43 +++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 3 files changed, 48 insertions(+) create mode 100644 .gitignore create mode 100644 csv2ankicards.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fd497cc --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +venv/ +*.pyc +__pycache__/ + diff --git a/csv2ankicards.py b/csv2ankicards.py new file mode 100644 index 0000000..1d71443 --- /dev/null +++ b/csv2ankicards.py @@ -0,0 +1,43 @@ +import csv +import genanki +import sys + +# Create a new model for our cards. This is necessary for genanki. +MY_MODEL = genanki.Model( + 1607392319, + "Simple Model", + fields=[ + {"name": "Question"}, + {"name": "Answer"}, + ], + templates=[ + { + "name": "Card 1", + "qfmt": "{{Question}}", + "afmt": "{{FrontSide}}
{{Answer}}", + }, + ]) + +def csv_to_anki(csv_path, output_path): + with open(csv_path, 'r', encoding='utf-8') as f: + reader = csv.reader(f) + my_deck = genanki.Deck(2059400110, "CSV Deck") + for row in reader: + question, answer = row + note = genanki.Note( + model=MY_MODEL, + fields=[question, answer] + ) + my_deck.add_note(note) + genanki.Package(my_deck).write_to_file(output_path) + +if __name__ == "__main__": + if len(sys.argv) != 3: + print("Usage: python convert.py ") + sys.exit(1) + + input_csv = sys.argv[1] + output_apkg = sys.argv[2] + csv_to_anki(input_csv, output_apkg) + print(f"Deck created at: {output_apkg}") + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5ef5d39 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +genanki==0.8.0