英文:
Python3 project to build a comic .epub file using ebooklib
问题
我正在尝试使用Python3和ebooklib模块将一组图像文件夹创建成.epub文件。
这本书是一本漫画书,所以在电子书中不需要文本,只需要从名为'images'的文件夹中提取图像。以下是我迄今为止的代码:
from ebooklib import epub
import os
# 设置epub文件
book = epub.EpubBook()
book.set_title("My Comic Book")
book.set_language('en')
# 向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)
# 将epub写入文件
epub.write_epub("my_comic.epub", book, {})
运行此代码将创建一个名为'my_comic.epub'的文件。但是,当我尝试在Apple iBooks中打开它时,出现错误:
无法打开"My Comic Book"
格式不正确,或者不是Apple Books可以打开的格式。
我做错了什么?
英文:
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?
答案1
得分: 1
你需要一个 xhtml 文件来列出所有的图片。以下是代码部分:
from ebooklib import epub
import os
# 设置 epub 文件
book = epub.EpubBook()
book.set_title("My Comic Book")
book.set_language('en')
content = ['<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]
# 将 epub 写入文件
epub.write_epub("my_comic.epub", book, {})
英文:
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, {})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论