I'm trying to take a folder of images and build a .epub file using Python3 and the ebooklib module.
The book is a comic book, so I don't need text in the ebook, just images from a folder called 'images'. Here is what I have so far:
from ebooklib import epub
import os
#set up the epub file
book = epub.EpubBook()
book.set_title("My Comic Book")
book.set_language('en')
#add images to epub
for filename in os.listdir("images"):
if filename.endswith(".jpg"):
image_file = open("images/" + filename, 'rb').read()
image = epub.EpubImage()
image.file_name = filename
image.content = image_file
book.add_item(image)
#write epub to file
epub.write_epub("my_comic.epub", book, {})
Running this creates a file called 'my_comic.epub'. But when I try to open it in Apple iBooks, I get the error:
Cannot open "My Comic Book" It is formatted incorrectly, or is not a format that Apple Books can open.
What am I doing wrong?
You need a xhtml file to list all your images. This seems to work.
from ebooklib import epub
import os
#set up the epub file
book = epub.EpubBook()
book.set_title("My Comic Book")
book.set_language('en')
content = [u'<html> <head></head> <body>']
for filename in os.listdir("images"):
if filename.endswith(".jpg"):
image_file = open("images/" + filename, 'rb').read()
image = epub.EpubImage()
image.file_name = "images/" + filename
image.content = image_file
book.add_item(image)
content.append('<img src="images/{}"/>'.format(filename))
content.append('</body> </html>')
c1 = epub.EpubHtml(title='Images', file_name='images.xhtml', lang='en')
c1.content=''.join(content)
book.add_item(c1)
book.spine = ['nav', c1]
#write epub to file
epub.write_epub("my_comic.epub", book, {})