I am trying to automatically import CSV files.
For this purpose, I am trying to use Anki’s built-in code as below.
But it causes an error.
What did I do wrong?
It works!
But only if I import the CSV file into my current collection.
I tried importing it into a temporary collection that I created programmatically and nothing changes. Unlike adding a new note, it does work. Only the CSV import does not.
(I’m trying to import a CSV file and control the deck and the notetype to which each note will be imported. Since I have no way to control this through Anki’s new import using a addon, I create a temporary collection as above and then import from it)
from anki.collection import ImportCsvRequest, ImportLogWithChanges, Collection
from aqt.utils import getFile, showText
from anki.utils import tmpdir
from aqt import mw
import os
short_col_name ="temp_for_import"
name = tmpdir() + "\\" + f"{short_col_name}"
colpath = name + "\collection.anki2"
try:
os.mkdir(name)
except FileExistsError:
pass
mw.temp_col = Collection(colpath)
model = mw.temp_col.models.current()
note = mw.temp_col.new_note(model)
mw.temp_col.add_note(note, mw.col.decks.selected())
path = getFile(mw, "getFile", None)
metadata = mw.temp_col.get_csv_metadata(path, None)
request = ImportCsvRequest(path=path, metadata=metadata)
print(len(mw.temp_col.find_notes("")))