55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
import zipfile
|
|
import sqlite3
|
|
import csv
|
|
import os
|
|
import tempfile
|
|
|
|
def extract_learning_progress(apkg_path, output_csv):
|
|
# Temporäres Verzeichnis zum Entpacken
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
# .apkg entpacken
|
|
with zipfile.ZipFile(apkg_path, 'r') as zip_ref:
|
|
zip_ref.extractall(temp_dir)
|
|
|
|
db_path = os.path.join(temp_dir, "collection.anki2")
|
|
if not os.path.exists(db_path):
|
|
print("Fehler: collection.anki2 nicht gefunden.")
|
|
return
|
|
|
|
# Verbindung zur Datenbank
|
|
conn = sqlite3.connect(db_path)
|
|
cursor = conn.cursor()
|
|
|
|
#cursor.execute("SELECT COUNT(*) FROM cards")
|
|
#print("Anzahl Karten in der Datenbank:", cursor.fetchone()[0])
|
|
|
|
cursor.execute("""
|
|
SELECT cards.id, notes.flds, cards.ivl, cards.reps, cards.type
|
|
FROM cards
|
|
JOIN notes ON cards.nid = notes.id
|
|
""")
|
|
results = cursor.fetchall()
|
|
print("Anzahl verknüpfte Karten:", len(results))
|
|
for row in results[:5]:
|
|
print(row)
|
|
|
|
|
|
|
|
# Daten auslesen: id, fälligkeitsdatum, Intervall, Wiederholungen, Lapses, Kartentyp
|
|
cursor.execute("""
|
|
SELECT id, nid, due, ivl, reps, lapses, type
|
|
FROM cards
|
|
""")
|
|
rows = cursor.fetchall()
|
|
|
|
# In CSV-Datei schreiben
|
|
with open(output_csv, mode='w', newline='', encoding='utf-8') as csvfile:
|
|
writer = csv.writer(csvfile)
|
|
writer.writerow(["card_id", "note_id", "due", "interval_days", "repetitions", "lapses", "type"])
|
|
writer.writerows(rows)
|
|
|
|
conn.close()
|
|
print(f"{len(rows)} Karten wurden exportiert nach: {output_csv}")
|
|
|
|
# Beispiel-Aufruf
|
|
extract_learning_progress("Langzeit.apkg", "lernfortschritt.csv") |