I am trying to solve the problem I posted here, i.e, creating multiple 1000 note .apkg from one .csv How I explain there, "I have a script that receives a .csv file and creates an apkg (anki program format). What I want is to create a .apkg deck for every certain number of lines the user enters. For example, if I have a deck.csv file with 4200 lines, and I choose to divide it into 1000 note decks it should generate the files:
deck 1-1000.apkg
deck 1001-2000.apkg
deck 2001-3000.apkg
deck 3001-4000.apkg
deck 4001-4200.apkg
The code I have tried now is.
import csv
import random
import genanki
# Filename of the data file
data_filename = str(input("Enter input file name with extension: "))
# Filename of the Anki deck to generate
deck_filename = data_filename.split('.')[0] + ".apkg"
# Title of the deck as shown in Anki
anki_deck_title = data_filename.split('.')[0]
# Name of the card model
anki_model_name = "Modelname"
# Create the deck model
model_id = random.randrange(1 << 30, 1 << 31)
style = """
.card {
font-family: arial;
font-size: 24px;
text-align: center;
color: black;
background-color: white;
"""
anki_model = genanki.Model(
model_id,
anki_model_name,
fields=[{"name": "front"}, {"name": "back"}],
templates=[
{
"name": "Card 1",
"qfmt": '{{front}}',
"afmt": '{{FrontSide}}<hr id="answer">{{back}}</p>',
},
],
css=style,
)
# The list of flashcards
anki_notes = []
with open(data_filename, "r", encoding="utf-8") as csv_file:
csv_reader = csv.reader(csv_file, delimiter=";")
for row in csv_reader:
anki_note = genanki.Note(
model=anki_model,
fields=[row[0], row[1]]
)
anki_notes.append(anki_note)
for subdeck in range(int((round((len(anki_notes)/1000)+0,5)))):
anki_deck = genanki.Deck(model_id, anki_deck_title+" "+str((subdeck-1)*1000+1)+"-"+str(subdeck*1000))
anki_package = genanki.Package(anki_deck)
# Add flashcards to the deck
for anki_note in range(1000):
anki_deck.add_note(anki_note)
# Save the deck to a file
anki_package.write_to_file(deck_filename)
print("Created deck with {} flashcards".format(len(anki_deck.notes)))
But I get the error
Enter input file name with extension: CLDR.csv
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-23-a8b71afba302> in <module>()
65
66 # Save the deck to a file
---> 67 anki_package.write_to_file(deck_filename)
68 print("Created deck with {} flashcards".format(len(anki_deck.notes)))
2 frames
/usr/local/lib/python3.6/dist-packages/genanki/deck.py in write_to_db(self, cursor, timestamp, id_gen)
59 models = json.loads(models_json_str)
60 for note in self.notes:
---> 61 self.add_model(note.model)
62 models.update(
63 {model.model_id: model.to_json(timestamp, self.deck_id) for model in self.models.values()})
AttributeError: 'int' object has no attribute 'model'
What i'm doing wrong?
I will guess but I think all your problem is beause you use two variables with similar name anki_notes
and anki_note
or beacuse you use the same name anki_note
it two places and you could use it in wrong way.
First you use anki_note
when you create notes
anki_note = Note(...)
and in anki_note
you have object which has field .model
But later you use anki_note
for integers.
for anki_note in range(1000):
and in next line you add anki_note
to anki_deck
like it would be Note
#for anki_note in range(1000):
anki_deck.add_note(anki_note)
So finally you add integers to anki_deck
instead of Notes
and later error shows in line self.add_model(note.model)
that it gets integer instead of object with field .model
So probably you should use anki_notes[anki_note]
in add_note
for anki_note in range(1000):
anki_deck.add_note( anki_notes[anki_note] )
Or you should use anki_notes
instead of range(1000)
for anki_note in anki_notes:
anki_deck.add_note( anki_note )
BTW:
I didn't test it but maybe instead of complex
for subdeck in range(int((round((len(anki_notes)/1000)+0,5)))):
str((subdeck-1)*1000+1) + "-" + str(subdeck*1000)
you need simpler
for subdeck in range(0, len(anki_notes), 1000):
str(subdeck+1) + "-" + str(subdeck+1000)