ebooklib icon indicating copy to clipboard operation
ebooklib copied to clipboard

set_cover is not use in python 3.7

Open arasHi87 opened this issue 6 years ago • 9 comments

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, {})

arasHi87 avatar Nov 05 '19 18:11 arasHi87

I'm having the same problem. The issue seems to be that the image file doesn't get copied int the ebook file. grafik

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

AWAS666 avatar Nov 16 '19 10:11 AWAS666

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?

GamerN131 avatar Mar 17 '20 19:03 GamerN131

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>)

james-c-barnes avatar Dec 12 '21 22:12 james-c-barnes

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.

aerkalov avatar Jan 09 '22 20:01 aerkalov

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

acover288 avatar Jul 04 '23 05:07 acover288