set_cover is not use in python 3.7
set_cover is not use, it didn't report any error, but i cant see the cover after I make the epub, I also try to unzip epub file to check if have jpg in it, and answer is yes, following is my code.
convert code
def txt2epub(data, path):
for name in data['content']:
book = epub.EpubBook()
book.set_identifier('{}{}'.format(data['title'], random.randint(0, 100000000)))
book.set_cover('cover.jpg', open(os.path.join(path, 'cover.jpg'), 'rb').read())
book.set_title(data['title'])
book.add_author(data['author'])
chapter_list = []
toc_list = []
for content in data['content'][name]:
chapter = epub.EpubHtml(title=content['title'],
file_name=content['vid'] + '.xhtml')
chapter.content = content['text']
book.add_item(chapter)
chapter_list.append(chapter)
toc_list.append(
epub.Link(content['vid'] + '.xhtml', content['title'],
content['vid']))
# toc_list.append(tuple([epub.Section('Languages'), chapter_list]))
book.toc = tuple(toc_list)
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())
book.spine = ['nav'] + chapter_list
epub.write_epub(os.path.join(path, name + '.epub'), book, {})
I'm having the same problem.
The issue seems to be that the image file doesn't get copied int the ebook file.

It only creates a file without extension which has the name you have given to it.
Edit: nvm I forgot to load the image as bytes... cover works now but the cover isn't the first chapter just as arasHi87 said
My cover is also not showing up as the first chapter, even when running the example for cover-creation provided (https://github.com/aerkalov/ebooklib/tree/master/samples).
In fact, it shows up two times at the end. In case of the example it shows after the text of the "About"-chapter and then again on the next page without the text.
But the cover is displayed as intended when loading the file in "Calibre" anyway.
Does anyone has a solution to this?
Just ran into this problem. I solved it by setting the cover OR adding an item. This fixed both problems; the cover not showing at the beginning and the file showing up two times. Hope this helps
if file_str == "cover.jpeg":
self.book.set_cover(<parameters here>)
else:
self.book.add_item(<parameters here>)
That sample file is a bit problematic because besides using cover image for cover it also inserts this cover image in the About page. This is why it seems the cover is shown at the end. When viewing book from Calibre for unknown reasons it does show up cover at the end.
The cover is not showing when you are flipping through pages because Cover image is by default marked in spine as non linear. The way to solve it right now is to manually add cover and do everything else which is done in set_cover. Example of that is here: https://github.com/booktype/Booktype/blob/master/lib/booktype/convert/epub/cover.py
That sample creates new cover page, adds cover file, changes metadata, spine and etc.
The cover shows at the end because in the spine entry linear="no". When you set linear="yes" the cover shows up where expected.
To make the change, the item needs is_linear = True. I didn't see a way to set this so I just override the property:
setattr(book.get_item_with_id('cover'), 'is_linear', True)
For more information see _write_opf_spine in epub.py