I’m trying to get a Python script (which runs as a cron job) to load cards directly into Anki rather than having to import them manually. I’ve tried a couple of ways, using col.add_note() and col.import_csv(), but in both cases if I run the script a second time a set of duplicate cards gets created. If I run the import from the GUI then Anki correctly flags duplicates and updates. Is there a way of getting the checking to work when importing via the CLI? Thanks in advance.
Since your cron script has access to col
, at the very least it ought to be possible to implement the duplicate checking yourself. So,
- parse your csv
- get the sort field for each row
- query collection for a duplicate note with the same sort field with
col.find_notes('...')
- if found, mark as note that should be updated or just not added. If not found add to list of notes to add
- add notes with
col.add_notes()
- (optional?) update notes with
col.update_notes()
for those that were flagged as duplicates (you’ll have to handle modifying each note field in existing notes in the script)
Many thanks for this but I’d hoped to be able to avoid having to code it myself, not least because I thought the capability must already exist somewhere! Further digging led me to TextImporter() which does what I want and returns a log entry with the numbers of notes imported, updated and not changed. In case it’s helpful to anyone else, my code is:
from anki.importing import TextImporter
ti = TextImporter(col, 'input file name')
ti.initMapping()
ti.run()
ti.log contains the results.
col.import_csv() has options to control duplicate resolution (If the legacy TextImporter doesn’t serve your needs).
OK, thanks - I hadn’t found those options so will keep import_csv() in mind but TextImporter is good until it stops working!
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.