Hello everyone,
I have been trying for hours to program a code that accesses an Excel file, gets the values of certain cells and uses them to create new flashcards based on them. The interaction with Anki is supposed to happen via the add-on AnkiConnect. The specific code I am using is posted down below. I inserted checks in it that should print error messages if any of the steps it carries out fail. It does not show any errors and prints that new flashcards are created just as I want them to. I also checked whether AnkiConnect is installed and enabled, and it is. Here is the log I get when running the code:
Excel file loaded successfully.
AnkiConnect is installed and running.
âŚ
Flashcard created: Liebe, Lieben â love
Flashcard created: Ferien â holidays, vacation (from school or university)
Flashcard created: Familie, Familien â family
Flashcard created: Burg, Burgen â castle
Flashcard created: Monster, Monster â monster
Flashcard created: England â England
Finished creating flashcards.
Still, the anki decks that I have tried it with never feature any new cards after I run the code. Does anyone have an idea why this could be? I have become so desperate that I opened the collection.anki2 file to check whether running the code changes anything, but I am not a computer guy and dont understand anything in there
Here is the code I am using. I am running it in a jupyter notebook
import pandas as pd
import requests
Load your Excel data
excel_file_path = râC:\Users\Brenda Elisa\Desktop\iLearnLang\Word Sheet.xlsxâ
try:
df = pd.read_excel(excel_file_path)
print(âExcel file loaded successfully.â)
except Exception as e:
print(f"Error loading Excel file: {e}")
raise
Define the range of rows to process (e.g., rows 5 to 10)
start_row = 5
end_row = 40
df = df.iloc[start_row - 1:end_row] # Adjust indices to start from 0
Specify the AnkiConnect API endpoint
anki_connect_url = â(http)://localhost:8765â
try:
# Check AnkiConnect
response = requests.get(f"{anki_connect_url}/version")
response.raise_for_status()
print(âAnkiConnect is installed and running.â)
except Exception as e:
print(f"AnkiConnect is not installed or running: {e}")
raise
Specify the deck name where you want to add flashcards
deck_name = âtestâ
Iterate through rows in your Excel file
for index, row in df.iterrows():
# Extract data from the Excel columns
word = row[âWordâ]
extra1 = row[âExtra1â]
extra2 = row[âExtra2â]
extra3 = row[âExtra3â]
translation = row[âTranslationâ]
# Concatenate non-empty values with a comma
extras = ', '.join([extra for extra in [extra1, extra2, extra3] if pd.notna(extra)])
# Concatenate "Word" with "Extras" if "Extras" is not empty
if extras:
word = f'{word}, {extras}'
# Create Anki flashcard with the concatenated string as the front and translation as the back
note = {
"deckName": deck_name,
"modelName": "Basic",
"fields": {
"Front": word,
"Back": translation
},
"tags": []
}
try:
# Add the note to Anki using AnkiConnect
response = requests.post(f"{anki_connect_url}/addNote", json=note)
response.raise_for_status()
print(f"Flashcard created: {word} -> {translation}")
except Exception as e:
print(f"Error creating flashcard: {e}")
raise
print(âFinished creating flashcards.â)