import os
import zipfile
from anki.collection import Collection
# Extract the .apkg file
def extract_apkg(apkg_file):
extract_dir = 'extracted_deck'
with zipfile.ZipFile(apkg_file, 'r') as zip_ref:
zip_ref.extractall(extract_dir)
# Return the path to the collection.anki2 file
return os.path.join(extract_dir, 'collection.anki2')
# Open the Anki collection
def open_collection(collection_path):
col = Collection(collection_path)
return col
# Remove cards that don't match 'Word = Hello'
def remove_non_matching_cards(col, word="Hello"):
# Get all note IDs in the collection
note_ids = col.find_notes('')
for note_id in note_ids:
note = col.get_note(note_id)
word_in_note = note.fields[0] # Assuming 'Word' is the first field
print(note) # if I try to print the card, I have only one system card (or I don't know what it is), but the deck has many cards
# If the word doesn't match, remove the note (and its cards)
if word_in_note != word:
col.remove(note)
print(f"Removed note with ID: {note_id} (Word: {word_in_note})")
# Main function
def main(apkg_file):
# Extract the .apkg file to get the collection.anki2 file
collection_path = extract_apkg(apkg_file)
print(f"Opened collection from: {collection_path}")
# Open the collection
col = open_collection(collection_path)
# Remove all cards except the one with Word = 'Hello'
remove_non_matching_cards(col)
# Save the collection after changes
col.save()
print("Changes saved to the collection.")
if __name__ == "__main__":
apkg_file = "english.apkg"
main(apkg_file)
seems like I found a way to open apkg file and remove not needed cards(or notes in my case, not sure if it is a correct way):
from anki.collection import ImportAnkiPackageRequest, Collection
collection = Collection("collection.anki2")
collection.import_anki_package(
ImportAnkiPackageRequest(
package_path="4000.apkg"
)
)
for c in collection.find_notes(""):
note = collection.get_note(c)
word = note.fields[0]
# print(note.card_ids)
# print(word)
if word != "arch":
collection.remove_notes([note.id])
but now how to save this collection as apkg deck please?