Change Deck of Cards

Hello guys,

i’m trying to make my own addon, so i want to programmatically change the deck of some cards.
Is this possible and how?

Thanks in advance,
Tobi7

You can use mw.col.set_deck(), passing it a list of card IDs and the ID of the deck:

cids = [1642081338943, ...]
mw.col.set_deck(cids, deck_id)
3 Likes

Sorry but that didn’t work for me, mw..col doesn’t have a method set_deck, but mw.col.decks, does.
So mw.col.decks.setDeck(cids, did) worked for me!

mw.co.set_deck() was introduced in 2.1.36 apparently, and mw.col.decks.setDeck was deprecated

3 Likes

You may wish to take a look at these pages dealing with setting up your IDE for code completion.

https://addon-docs.ankiweb.net/editor-setup.html

This is a good way to discover what you need, although digging around in the source code is also necessary a lot of the time - the places to start are usually /qt/aqt/ or /pylib/anki/

1 Like

I’m getting an error when I try to use mw.col.set_deck() like you described
I correctly assign the 2 args for the card_IDs and the target deck_ID

but I get this error:
mw.col.decks.setDeck(search, deck_dest_id)
AttributeError: ‘NoneType’ object has no attribute ‘col’

this is my import statement

from aqt import mw

how would I import the collection so that mw can use it?

Importing and using mw won’t really work outside of the context of a live, Anki runtime environment. I.e., you can’t just start up a Python shell with the dependencies installed and use mw. In order to get a Python shell that is attached to the Anki runtime environment, you can use the debug console. That’s the quickest way, but I recommend reading the add-on docs, running Anki from the command line, and learning to use aqt.debug in our add-on code to stop execution at a breakpoint for a more user friendly way of getting into an interactive Python shell.

2 Likes

turns out I’m really slow and the solution was just to call the set_deck method straight from the collection itself

1 Like