如何解决使用 ebooklib 时如何更改封面文件中的 html?
我面临的问题是我不喜欢 ebooklib 为我的封面制作的 html 文件。本来想用svg标签等,其他的Ebooklib项目,设置内容就是如何改html,教程里有说明。
在我的代码中,我尝试
book = epub.EpubBook()
book.set_cover(cover_image,open(cover_image,'rb').read())
cover_page = book.get_item_with_id('cover')
cover_page.content = some_html
但是以这种方式更改内容似乎对封面没有任何作用。
解决方法
从https://github.com/aerkalov/ebooklib/blob/master/ebooklib/epub.py开始,ebooklib 的 EpubCoverHtml 在它的 get_content 方法中设置了内容,但是它似乎没有使用它,并且似乎没有一种方法可以编辑内容,因为它总是在该方法中重新生成.
def get_content(self):
"""
Returns content for cover page as HTML string. Content will be of type 'str' (Python 2) or 'bytes' (Python 3).
:Returns:
Returns content of this document.
"""
self.content = self.book.get_template('cover')
tree = parse_string(super(EpubCoverHtml,self).get_content())
tree_root = tree.getroot()
images = tree_root.xpath('//xhtml:img',namespaces={'xhtml': NAMESPACES['XHTML']})
images[0].set('src',self.image_name)
images[0].set('alt',self.title)
tree_str = etree.tostring(tree,pretty_print=True,encoding='utf-8',xml_declaration=True)
return tree_str
要真正修改封面html的样子,好像还得修改模板:
cover_template = six.b('''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" lang="en" xml:lang="en">
<head>
<style>
body { margin: 0em; padding: 0em; }
img { max-width: 100%; max-height: 100%; }
</style>
</head>
<body>
<img src="" alt="" />
</body>
</html>''')
book.set_template(name='cover',value=cover_template)
注意:这实际上是默认模板。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。