def createPackage(self, deck, path, includeSched): collection = self.collection() if collection is not None: deck = collection.decks.byName(deck) if deck is not None: exporter = AnkiPackageExporter(collection) exporter.did = deck["id"] exporter.includeSched = includeSched exporter.exportInto(path) return True return False
def main(): # opens the collection log.info('open collection: %s', options.file) cwd = os.getcwd() col = anki.Collection(path=options.file) work_dir = os.getcwd() os.chdir(cwd) kd = KanjiDamage(col, log) # should update kanji damage deck? if options.reset_kd: kd.reset(options.kd_file) # finds the kanji damage deck and model if not kd.get_deck(): sys.exit('{0}: error: {1}'.format( sys.argv[0], 'couldn\'t find KanjiDamage[ Reordered] deck in the collection, try using option -r to import it' )) kd_model = kd.get_model() if not kd_model: sys.exit('{0}: error: {1}'.format( sys.argv[0], 'couldn\'t find KanjiDamage model in the collection, try using option -r to import it' )) # updates kanji damage deck if options.update_kd: log.info('updating %s...', kd_model['name']) # removes media files if options.force_download: shutil.rmtree(os.path.join(col.media.dir(), 'assets'), ignore_errors=True) shutil.rmtree(os.path.join(col.media.dir(), 'visualaids'), ignore_errors=True) kd.update() # recreates the kanji damage words deck _, kdw_deck = kdw_create(col, kd) log.info('writing output file %s...', options.output) exporter = AnkiPackageExporter(col) exporter.includeSched = False exporter.includeMedia = True exporter.includeTags = True exporter.did = kdw_deck['id'] out_path = os.path.join(os.getcwd(), options.output) os.chdir(work_dir) exporter.exportInto(out_path) log.info('all is well!') col.close()
def export_deck(did): # inspired by aqt.exporting.ExportDialog.accept exporter = AnkiPackageExporter(mw.col) exporter.includeSched = False exporter.includeMedia = gc("include media that starts with underscore" ) # get _styles_for_syntax_highlighting.css exporter.includeTags = False exporter.includeHTML = False exporter.did = did now = time.strftime("-%Y-%m-%d@%H-%M-%S", time.localtime(time.time())) while 1: file = getSaveFile( mw, # parent _("Export"), # windowtitle "debug_export_all_notetypes", # dir_description - used to remember last user choice for dir exporter.key, # key exporter.ext, # ext 'export_all_notetypes__%s.apkg' % now) # filename if not file: return if checkInvalidFilename(os.path.basename(file), dirsep=False): continue break if file: mw.progress.start(immediate=True) try: f = open(file, "wb") f.close() except (OSError, IOError) as e: showWarning(_("Couldn't save file: %s") % str(e)) else: os.unlink(file) exportedMedia = lambda cnt: mw.progress.update(label=ngettext( "Exported %d media file", "Exported %d media files", cnt) % cnt ) addHook("exportedMediaFiles", exportedMedia) exporter.exportInto(file) remHook("exportedMediaFiles", exportedMedia) tooltip("Exporting finished", 3000) finally: mw.progress.finish()
def export(deck,filename,tmpPath = '',mediaPath = '',ignoreMedia = False): wdir = os.getcwd() def cleanFolder(): os.chdir(wdir) # Cleans out the old tmp collection if os.path.exists(tmpPath + 'tmp.media'): shutil.rmtree(tmpPath + 'tmp.media') for f in ['tmp.media.db2','tmp.anki2','tmp.anki2-journal']: if os.path.exists(tmpPath + f): try: os.remove(tmpPath + f) except: continue cleanFolder() # Makes a new one tcol = Collection(tmpPath + 'tmp.anki2') os.chdir(wdir) # Copies media over if not ignoreMedia: copytree(mediaPath,tmpPath + 'tmp.media') os.chdir(wdir) # Sets up the decks and pulls them in def makeDeck(parent,prefix,deck): name = deck.csvname csvfile = "%s%s%s.csv" % (tmpPath,prefix,name) if not os.path.exists(csvfile) and deck.cardType != None: print('Skipping deck "%s" because no file "%s" was found.' % (name, csvfile)) return # raise Exception('No csv file "' + csvfile + '" found.') did = tcol.decks.id(parent + deck.name) d = tcol.decks.get(did) tcol.decks.select(did) confId = tcol.decks.confId(parent + deck.name, cloneFrom=deck.conf) if not deck.cardType: conf = tcol.decks.getConf(confId) conf['new']['perDay'] = 999 tcol.decks.updateConf(conf) elif deck.perDay: conf = tcol.decks.getConf(confId) conf['new']['perDay'] = deck.perDay tcol.decks.updateConf(conf) tcol.decks.setConf(d,confId) if deck.cardType: ct = deck.cardType if not tcol.models.byName(ct.name): m = tcol.models.new(ct.name) m['req'] = [[0, 'all', [0]]] m['css'] = ct.css() m['tmpls'] = [ { 'name': 'Card 1', 'qfmt': ct.front(), 'afmt': ct.back(), 'bfont': 'Lucida Sans Unicode', 'bamft': '', 'bqmft': '', 'ord': 0, 'did': None, 'bsize': 12 } ] tcol.models.add(m) for i,field in enumerate(ct.fields): f = tcol.models.newField(field.anki_name) f['ord'] = i tcol.models.addField(m,f) else: m = tcol.models.byName(ct.name) # So that we can reuse already-present models # todo: this doesn't actually work but would be a big part of # updating # if m['id'] != ct.mid: # m = tcol.models.get(m['id']) # m['id'] = ct.mid # m.save(m) tcol.save() m['did'] = did tcol.decks.select(did) ti = TextImporter(tcol,csvfile) ti.model = m ti.allowHTML = True ti.initMapping() ti.delimiter = "\t" ti.updateDelimiter() ti.run() tcol.save() for sd in deck.subdecks: makeDeck(parent + deck.name + '::',prefix + name + '-', sd) makeDeck('','',deck) os.chdir(wdir) apkge = AnkiPackageExporter(tcol) apkge.includeSched = True apkge.exportInto(filename) cleanFolder()