Spaces:
Build error
Build error
File size: 1,392 Bytes
b0d689c | 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 | import genanki
import csv
import random
# anki: csv轉anki
# https://www.volcengine.com/theme/9746040-P-7-1
def Convert(csv_file, deck_name):
# Create a Genanki model
model_id = random.randint(1000000000, 9999999999)
model = genanki.Model(
model_id,
"基本型",
fields=[
{"name": "正面"},
{"name": "背面"},
],
templates=[
{
"name": "卡片 1",
"qfmt": "{{Question}}",
"afmt": "{{FrontSide}}<br>{{Answer}}",
},
],
)
# print(model_id)
# Create an Anki deck
deck_id = random.randint(1000000000, 9999999999)
deck = genanki.Deck(
deck_id,
deck_name
)
# print(deck_id)
# Read the CSV file and add cards to the deck
with open(csv_file, "r", encoding="utf-8") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
question = row["Question"]
answer = row["Answer"]
note = genanki.Note(
model = model,
fields = [question, answer]
)
deck.add_note(note)
# Save the deck to an Anki deck file (.apkg)
package = genanki.Package(deck)
apkg_file = csv_file.split(".")
apkg_file = apkg_file[0]+".apkg"
package.write_to_file(apkg_file)
return apkg_file |