def import_command(apkgs, collection_path=None, profile=None): collection_path = create_check_collection_path(collection_path, profile) # I need to do this because creating a collection changes the current path. # Anki should fix that I think... apkgs = [os.path.abspath(i) for i in apkgs] # Create collection. col = Collection(collection_path) # Import collection. for a in apkgs: AnkiPackageImporter(col, a).run() # Close collection. col.close()
def importDecks(): if not os.path.isdir(DECK_DIR): raise (ValueError("{} is not a directory".format(DECK_DIR))) os.chdir(os.path.join(os.path.split(__file__)[0], "user_files")) # NOTE this loop is because ankdown can't easily create multiple decks # - genanki ends up lumping them all together. Maybe one day when that # bug is fixed, we can call ankdown once. for (d, subds, fns) in os.walk(DECK_DIR): if not any([fn.endswith(".md") for fn in fns]): continue package_name = os.path.basename(d) + ".apkg" subprocess.run([ANKDOWN, "-r", d, "-p", package_name]) AnkiPackageImporter(mw.col, package_name).run()
def write_to_collection_from_addon(self): """ Write to local collection. *Only usable when running inside an Anki addon!* Only tested on Anki 2.1. This writes to a temporary file and then calls the code that Anki uses to import packages. Note: the caller may want to use mw.checkpoint and mw.reset as follows: # creates a menu item called "Undo Add Notes From MyAddon" after this runs mw.checkpoint('Add Notes From MyAddon') # run import my_package.write_to_collection_from_addon() # refreshes main view so new deck is visible mw.reset() Tip: if your deck has the same name and ID as an existing deck, then the notes will get placed in that deck rather than a new deck being created. """ from aqt import mw # main window from anki.importing.apkg import AnkiPackageImporter with tempfile.NamedTemporaryFile() as f: self.write_to_file(f.name) AnkiPackageImporter(mw.col, f.name).run()
sys.path.insert(0, "/usr/share/anki/") import anki import anki.sync from anki.importing.apkg import AnkiPackageImporter sys.path.insert(0, "../genanki/") import genanki.util as util deck_name = "General::IT" ac = anki.Collection("/home/tr/.local/share/Anki2/test/collection.anki2") deck_id = ac.decks.id(deck_name) ac.decks.select(deck_id) importer = AnkiPackageImporter(ac, "/tmp/output.apkg") print("Import") importer.run() for i in importer.log: if i.startswith("[Identical]"): continue if i and i.strip(): print("> " + i) ac.save() print()
import sys from datetime import datetime sys.path.append('/usr/share/anki') from anki import Collection from anki.importing.apkg import AnkiPackageImporter profile = sys.argv[1] if len(sys.argv) > 1 else "willy" collection_path = f"/home/guiferviz/.local/share/Anki2/{profile}/collection.anki2" col = Collection(collection_path) #res = col.db.list("select * from cards") package = sys.argv[2] if len(sys.argv) > 2 else "regex" t = datetime.now() AnkiPackageImporter(col, f"/home/guiferviz/code/mnemocards/{package}.apkg").run() t = datetime.now() - t query = f""" select n.id from notes n left join cards c on c.nid = n.id left join ( select did, max(n.mod) mod from notes n left join cards c on c.nid = n.id group by did ) d on d.did = c.did