Skip to content

Instantly share code, notes, and snippets.

@Inom-Turdikulov
Last active January 30, 2022 16:18
Show Gist options
  • Save Inom-Turdikulov/daaf62fb74a5b1bdd6486da256651ea7 to your computer and use it in GitHub Desktop.
Save Inom-Turdikulov/daaf62fb74a5b1bdd6486da256651ea7 to your computer and use it in GitHub Desktop.
Python script, to move cards from one AnkiDeck to another AnkiDeck. Check comments for required dependencies.
#!/usr/bin/env python3
# You need py-notifier package installed
# https://github.com/YuriyLisovskiy/pynotifier
# and also AnkiConnect extension
# https://ankiweb.net/shared/info/2055492159
import json
import sys
import urllib.request
from pynotifier import Notification
import argparse
parser = argparse.ArgumentParser(description='Move words from one deck to another.')
parser.add_argument('word', type=str, help='word which need to move')
args = parser.parse_args()
def request(action, **params):
return {'action': action, 'params': params, 'version': 6}
def invoke(action, **params):
request_json = json.dumps(request(action, **params)).encode('utf-8')
response = json.load(urllib.request.urlopen(
urllib.request.Request('http://localhost:8765', request_json)))
if len(response) != 2:
raise Exception('response has an unexpected number of fields')
if 'error' not in response:
raise Exception('response is missing required error field')
if 'result' not in response:
raise Exception('response is missing required result field')
if response['error'] is not None:
raise Exception(response['error'])
return response['result']
if __name__ == '__main__':
from_deck = '14000+ English Words'
to_deck = '_My English'
cards = invoke('findCards', query=f"English:{args.word}")
to_move_cards = []
if cards:
cards_info = invoke('cardsInfo', cards=cards)
for card in cards_info:
if card['deckName'] == from_deck:
to_move_cards.append(card['cardId'])
if to_move_cards:
result = invoke('changeDeck', cards=to_move_cards, deck=to_deck)
Notification(title='Successfully moved cards',
description=to_move_cards).send()
sys.exit(0)
Notification(title='No cards found', description=to_move_cards).send()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment