kerrickstaley/genanki: A Python 3 library for generating Anki decks A Python 3 library…
- User
kerrickstaley/genanki: A Python 3 library for generating Anki decks A Python 3 library for generating Anki decks. Contribute to kerrickstaley/genanki development by crea...... genanki: A Library for Generating Anki Decks genanki allows you to programatically generate decks in Python 3 for Anki, a popular spaced-repetition flashcard program. Please see below for concepts and usage.
This library and its author(s) are not affiliated/associated with the main Anki project in any way.
Notes The basic unit in Anki is the Note, which contains a fact to memorize. Notes correspond to one or more Cards.
Here's how you create a Note:
my_note = genanki.Note( model=my_model, fields=['Capital of Argentina', 'Buenos Aires'])
You pass in a Model, discussed below, and a set of fields (encoded as HTML).
Models A Model defines the fields and cards for a type of Note. For example:
my_model = genanki.Model( 1607392319, 'Simple Model', fields=[ {'name': 'Question'}, {'name': 'Answer'}, ], templates=[ { 'name': 'Card 1', 'qfmt': '{{Question}}', 'afmt': '{{FrontSide}}{{Answer}}', }, ])
This note-type has two fields and one card. The card displays the Question field on the front and the Question and Answer fields on the back, separated by a . You can also pass a css argument to Model() to supply custom CSS.
You need to pass a model_id so that Anki can keep track of your model. It's important that you use a unique model_id for each Model you define. Use import random; random.randrange(1 << 30, 1 << 31) to generate a suitable model_id, and hardcode it into your Model definition.
Generating a Deck/Package To import your notes into Anki, you need to add them to a Deck:
my_deck = genanki.Deck( 2059400110, 'Country Capitals')
my_deck.add_note(my_note)
Once again, you need a unique deck_id that you should generate once and then hardcode into your .py file.
Then, create a Package for your Deck and write it to a file:
genanki.Package(my_deck).write_to_file('output.apkg')
You can then load output.apkg into Anki using File -> Import...
Media Files To add sounds or images, set the media_files attribute on your Package:
my_package = genanki.Package(my_deck) my_package.media_files = ['sound.mp3', 'images/image.jpg']
media_files should have the path (relative or absolute) to each file. To use them in notes, first add a field to your model, and reference that field in your template:
my_model = genanki.Model( 1091735104, 'Simple Model with Media', fields=[ {'name': 'Question'}, {'name': 'Answer'}, {'name': 'MyMedia'}, # ADD THIS ], templates=[ { 'name': 'Card 1', 'qfmt': '{{Question}}{{MyMedia}}', # AND THIS 'afmt': '{{FrontSide}}{{Answer}}', }, ])
Then, set the MyMedia field on your card to [sound:sound.mp3] for audio and for images.
- Assistant
- User
给出一个完整的带有图片的代码例子
- Assistant
为了给出一个完整的示例,我们将遵循
genanki库的使用方式创建一个包含图片的Anki卡片。假设我们要创建一个问答卡片,问题是“旗帜是哪个国家的?”答案是“加拿大”,并且我们有加拿大国旗的图片作为一个视觉提示。首先确保你已经安装了
genanki库。如果没有,可以通过运行以下命令来安装它:pip install genanki接下来,我们将编写一个Python脚本来创建一个Anki包。假设国旗图片的文件名是
canada_flag.jpg并且它位于脚本的同一文件夹下。import genanki # 创建一个模型,这个模型包含一个问题字段和一个答案字段 # 以及一个媒体字段用于展示图片 my_model = genanki.Model( 123456789, 'Simple Model with Image', fields=[ {'name': 'Question'}, {'name': 'Answer'}, {'name': 'FlagImage'}, ], templates=[ { 'name': 'Card 1', 'qfmt': '{{Question}}<br>{{FlagImage}}', # 面部显示问题和图片 'afmt': '{{FrontSide}}<hr id=answer>{{Answer}}', # 背面显示面部内容和答案 }, ]) # 创建一个Note,包含实际的数据 my_note = genanki.Note( model=my_model, fields=['旗帜是哪个国家的?', '加拿大', '<img src="canada_flag.jpg">']) # 包含图片的字段 # 创建一个Deck my_deck = genanki.Deck( 2059400110, '国家旗帜') # 将Note添加到Deck中 my_deck.add_note(my_note) # 创建一个包含Deck的Package,记得添加图片文件 my_package = genanki.Package(my_deck) my_package.media_files = ['canada_flag.jpg'] # 保存生成的包为一个文件,准备导入Anki my_package.write_to_file('country_flags.apkg')