EbookLib is a Python library for managing EPUB2/EPUB3 files. It’s capable of reading and writing EPUB files programmatically.
遗憾的是,官方示例很多地方没讲清楚,比如怎么添加图片等。只能自己琢磨了,以下是我的方案。还有大量问题待解决。
# add images
with open("img.txt", "r") as frit:
for line in frit:
path = line.strip()
img_name = "images/"+path.split("/")[-1]
img_uid = "img_"+path.split("/")[-2]
ext_name = path.split(".")[-1]
if ext_name == "jpg":
img_type = "image/jpeg"
if ext_name == "png":
img_type = "image/png"
with open(path, "rb") as frp:
content_img = frp.read()
img_item = epub.EpubItem(uid=img_uid, file_name=img_name, media_type=img_type, content=content_img)
book.add_item(img_item)