Python3项目,使用ebooklib构建漫画.epub文件。

huangapple go评论71阅读模式
英文:

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(&quot;My Comic Book&quot;)
book.set_language(&#39;en&#39;)

content = [u&#39;&lt;html&gt; &lt;head&gt;&lt;/head&gt; &lt;body&gt;&#39;]

for filename in os.listdir(&quot;images&quot;):
    if filename.endswith(&quot;.jpg&quot;):
        image_file = open(&quot;images/&quot; + filename, &#39;rb&#39;).read()
        image = epub.EpubImage()
        image.file_name = &quot;images/&quot; + filename
        image.content = image_file
        book.add_item(image)
        content.append(&#39;&lt;img src=&quot;images/{}&quot;/&gt;&#39;.format(filename))

content.append(&#39;&lt;/body&gt; &lt;/html&gt;&#39;)
c1 = epub.EpubHtml(title=&#39;Images&#39;, file_name=&#39;images.xhtml&#39;, lang=&#39;en&#39;)
c1.content=&#39;&#39;.join(content)

book.add_item(c1)
book.spine = [&#39;nav&#39;, c1]

#write epub to file
epub.write_epub(&quot;my_comic.epub&quot;, book, {})

huangapple
  • 本文由 发表于 2023年2月8日 20:50:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定