anki-csv2ankicards/csv2ankicards.py

50 lines
1.3 KiB
Python
Raw Normal View History

2023-09-07 11:05:57 +00:00
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}}<hr id='answer'>{{Answer}}",
},
])
def csv_to_anki(csv_path, output_path):
with open(csv_path, 'r', encoding='utf-8') as f:
reader = csv.reader(f)
2023-09-07 11:20:31 +00:00
# Skipping the header row
next(reader, None)
2023-09-07 11:05:57 +00:00
my_deck = genanki.Deck(2059400110, "CSV Deck")
for row in reader:
2023-09-07 11:20:31 +00:00
# Use row directly without splitting
question = row[0]
answer = ",".join(row[1:])
2023-09-07 11:05:57 +00:00
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 <input_csv> <output_apkg>")
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}")