kerrickstaley / genanki

A Python 3 library for generating Anki decks
MIT License
2.06k stars 161 forks source link

A problem in media #68

Closed woodsonchilduniverse closed 3 years ago

woodsonchilduniverse commented 3 years ago

I use a Python module genanki to generate cards.But the image is always broken in the card. My code are as follow . ‘’’ import genanki

my_model = genanki.Model( 1091735104, ‘Simple Model with Media’, fields=[ {‘name’: ‘Question’}, {‘name’: ‘Answer’}, {‘name’: ‘MyMedia’}, {‘name’: ‘MyAnswerMedia’} # ADD THIS ], templates=[ { ‘name’: ‘Card 1’, ‘qfmt’: ‘{{Question}} {{MyAnswerMedia}}’, # AND THIS ‘afmt’: ‘{{FrontSide}}

{{MyMedia}} {{Answer}}’, }, ]) deckname = “newDeck” decknumber = 987655 my_deck = genanki.Deck( decknumber, deckname)

my_note=genanki.Note( model=my_model, fields=[“test”,“test”, ‘’,“test”]) my_deck.add_note(my_note)

my_package = genanki.Package(my_deck) my_package.media_files = [ “img_2991.jpg”] genanki.Package(my_deck).write_to_file(deckname + r".apkg") ‘’’

By the way when I check the media file within apkg file and open it with notebook,it simply contain a {}.Can someone help me?

remiberthoz commented 3 years ago

Hey, I am a fellow user of genanki.

You have the issue because of the last three lines:

my_package = genanki.Package(my_deck)
my_package.media_files = [ “img_2991.jpg”]
genanki.Package(my_deck).write_to_file(deckname + r".apkg")

should read:

my_package = genanki.Package(my_deck)
my_package.media_files = [ “img_2991.jpg”]
my_package.write_to_file(deckname + r".apkg")  # <--- Modify this line

Because in your version, you create a package (first line) and add a media file to it (second line). And then (third line) you recreate a package and apply write_to_file to it. The package you create in the third line does not contain the image.

woodsonchilduniverse commented 3 years ago

Hey, I am a fellow user of genanki.

You have the issue because of the last three lines:

my_package = genanki.Package(my_deck)
my_package.media_files = [ “img_2991.jpg”]
genanki.Package(my_deck).write_to_file(deckname + r".apkg")

should read:

my_package = genanki.Package(my_deck)
my_package.media_files = [ “img_2991.jpg”]
my_package.write_to_file(deckname + r".apkg")  # <--- Modify this line

Because in your version, you create a package (first line) and add a media file to it (second line). And then (third line) you recreate a package and apply write_to_file to it. The package you create in the third line does not contain the image.

Thanks for your reply.What should I do to modify the last line?It seems to me ,I have to add this line.

woodsonchilduniverse commented 3 years ago

And why the package I recreated still contains text?

remiberthoz commented 3 years ago

Hey,

So you want to replace

​​genanki​.​Package​(​my_deck​).​write_to_file​(​deckname​ ​+​ ​r".apkg"​)

With:

​​my_package​.​write_to_file​(​deckname​ ​+​ ​r".apkg"​)

So that's the one I put in my previous message. You don't have to modify it, sorry if that was unclear.


The package you created with your initial code did contain text but no image. This is normal, I'll annotate your code to explain:

# Create a Package named my_package with the content of my_deck:
​my_package​ ​=​ ​genanki​.​Package​(​my_deck​)
# Add img_2991.jpg as a media file to my_package:
​my_package​.​media_files​ ​=​ [ “​img_2991​.​jpg​”]
# Create a Package with the content of my_deck and write it to a file:
​genanki​.​Package​(​my_deck​).​write_to_file​(​deckname​ ​+​ ​r".apkg"​)

Whereas in my code:

# Create a Package named my_package with the content of my_deck:
​my_package​ ​=​ ​genanki​.​Package​(​my_deck​)
# Add img_2991.jpg as a media file to my_package:
​my_package​.​media_files​ ​=​ [ “​img_2991​.​jpg​”]
# Write my_package to a file:
​my_package.write_to_file(deckname + r".apkg")
woodsonchilduniverse commented 3 years ago

Hey,

So you want to replace

​​genanki​.​Package​(​my_deck​).​write_to_file​(​deckname​ ​+​ ​r".apkg"​)

With:

​​my_package​.​write_to_file​(​deckname​ ​+​ ​r".apkg"​)

So that's the one I put in my previous message. You don't have to modify it, sorry if that was unclear.

The package you created with your initial code did contain text but no image. This is normal, I'll annotate your code to explain:

# Create a Package named my_package with the content of my_deck:
​my_package​ ​=​ ​genanki​.​Package​(​my_deck​)
# Add img_2991.jpg as a media file to my_package:
​my_package​.​media_files​ ​=​ [ “​img_2991​.​jpg​”]
# Create a Package with the content of my_deck and write it to a file:
​genanki​.​Package​(​my_deck​).​write_to_file​(​deckname​ ​+​ ​r".apkg"​)

Whereas in my code:

# Create a Package named my_package with the content of my_deck:
​my_package​ ​=​ ​genanki​.​Package​(​my_deck​)
# Add img_2991.jpg as a media file to my_package:
​my_package​.​media_files​ ​=​ [ “​img_2991​.​jpg​”]
# Write my_package to a file:
​my_package.write_to_file(deckname + r".apkg")

I really appreciate your help.You are so kind.I totally understand it.