kerrickstaley / genanki

A Python 3 library for generating Anki decks
MIT License
2.06k stars 161 forks source link

Anki Deck cannot import #38

Closed jasoncarrillo0 closed 5 years ago

jasoncarrillo0 commented 5 years ago

When creating the deck, upon import, Anki says the notes have been added yet I cannot find them anywhere. Here's the code:

from openpyxl import load_workbook
import genanki
import random

"""
1. be able to generate a random number that's really big without repitions
2. setup note type
3. setup the model
4. make a deck to fill
5. figure out note types

"""
beg = 1000000000
end = 9999999999
id_list = []
for i in range(0, 5000):
    randomNum = random.randint(beg,end)
    while randomNum in id_list:
        randomNum = random.randint(beg, end)
    id_list.append(randomNum)

model = genanki.Model(
  id_list[-1],
  'Simple Model',
  fields=[
    {'name': 'Meaning'},
    {'name': 'Arabic Spelling'},
    {'name': 'Phonetic Spelling'},
    {'name': 'Audio'},
  ],
  templates=[
      {
        'name': 'Only Card',
          'qfmt': 'How do you say:<div style="font-family: Arial; font-size: 40px; padding: 20px;">{{Arabic Spelling}}</div>',
          'afmt': '{{FrontSide}}<hr id=answer><div style="font-family: Arial; font-size: 20px; padding: 20px;">{{Meaning}}</div><div style="font-family: Arial; font-size: 20px; padding: 20px;"><em>{{Phonetic Spelling}}</em></div><div style="font-family: Arial; font-size: 20px; padding: 20px;">{{Audio}}</div>',
      },
  ])
del id_list[-1]

my_deck = genanki.Deck(
  id_list[-1],
  'test')
del id_list[-1]

wb = load_workbook("all_words.xlsx")
sheet = wb.get_sheet_by_name('ALL WORDS')
englishWordList = []

for col_cells in sheet.iter_cols(min_row=2, min_col=1, max_col=1):
    for cell in col_cells:
        englishWordList.append(cell.value)

i = 0
for word in englishWordList:
    note = genanki.Note(
        sort_field=id_list[i],
        model=model,
        fields=[word, "", "", ""]
    )
    my_deck.add_note(note)
    i = i + 1
i = 0

genanki.Package(my_deck).write_to_file("testOutput.apkg")

Anki adds all the notes (which have no cards made) yet I can't find them inside Anki. I thought maybe it's because of my add-ons, yet from the readme file it's unclear how to use the .write_to_collection_from_addon() method. I don't know how to use the method.

When I close and reopen Anki, it says there is a problem with the database and tells me to "Check Database". Once I do this, it deletes all the notes that were added from my script. What's the best way to use the method, and/or what is the solution to this issue? I've tried looking everywhere. Screen Shot 2019-09-03 at 5 13 39 PM Screen Shot 2019-09-03 at 5 14 20 PM

jasoncarrillo0 commented 5 years ago

SOLVED: I suggest requiring any field part of the front formatting of a card to be required. If it's blank as in above, the card will not be generated causing notes to be created that are inaccessible. There may have been an issue with my sort_field = id_list[i] as well. For more explanation see this post: https://www.reddit.com/r/Anki/comments/czi68k/genanki_issues_imported_notes_are_there_but/