Collection

This is the starting point for most end-users. The Collection class loads the Anki collection and provides access to its notes, cards and reviews as pandas DataFrame objects.

class ankipandas.collection.Collection(path=None, user=None)[source]

Bases: object

__init__(path=None, user=None)[source]

Initialize Collection object.

Parameters

Examples:

from ankipandas import Collection

# Let ankipandas find the db for you
col = Collection()

# Let ankipandas find the db for this user (important if you have
# more than one user account in Anki)
col = Collection(user="User 1")

# Specify full path to Anki's database
col = Collection("/full/path/to/collection.anki2")

# Specify partial path to Anki's database and specify user
col = Collection("/partial/path/to/collection", user="User 1")
property path: pathlib.Path

Path to currently loaded database

property db: sqlite3.Connection

Opened Anki database. Make sure to call db.close() after you’re done. Better still, use contextlib.closing.

property notes: ankipandas.ankidf.AnkiDataFrame

Notes as ankipandas.ankidf.AnkiDataFrame.

property cards: ankipandas.ankidf.AnkiDataFrame

Cards as ankipandas.ankidf.AnkiDataFrame.

property revs: ankipandas.ankidf.AnkiDataFrame

Reviews as ankipandas.ankidf.AnkiDataFrame.

empty_notes()[source]

Similar ankipandas.ankidf.AnkiDataFrame to notes, but without any rows.

empty_cards()[source]

Similar ankipandas.ankidf.AnkiDataFrame to cards, but without any rows.

empty_revs()[source]

Similar ankipandas.ankidf.AnkiDataFrame to revs, but without any rows.

summarize_changes(output='print') Optional[Dict[str, dict]][source]

Summarize changes that were made with respect to the table as loaded from the database. If notes/cards/etc. were not loaded at all (and hence also definitely not modified), they do not appear in the output.

Parameters

output – Output mode: ‘print’ (default: print) or ‘dict’ (return as dictionary of dictionaries of format {<type (cards/notes/...)>: {<key>: <value>}}.

Returns

None or dictionary of dictionaries

write(modify=False, add=False, delete=False, backup_folder: Optional[Union[pathlib.PurePath, str]] = None)[source]

Creates a backup of the database and then writes back the new data.

Danger

The switches modify, add and delete will run additional cross-checks, but do not rely on them to 100%!

Warning

It is recommended to run summarize_changes() before to check whether the changes match your expectation.

Note

Please make sure to thoroughly check your collection in Anki after every write process!

Parameters
  • modify – Allow modification of existing items (notes, cards, etc.)

  • add – Allow adding of new items (notes, cards, etc.)

  • delete – Allow deletion of items (notes, cards, etc.)

  • backup_folder – Path to backup folder. If None is given, the backup is created in the Anki backup directory (if found).

Returns

None