Open ehafizoglu opened 1 year ago
I have the same question. Have you solved that?
Yes I have solved that with "html2epub". I have used both "html2epub" and "ebooklib". "html2epub" is used for the loop. After the epub file is created, I used ebooklib to set the Cover Image and other images.
This is a very basic sample how to do it. Let's imagine we have this script and we have bunch of HTML files in the directory called "files".
Execute this:
python html2epub ./files/
This is very basic. You can sort the files according to some criteria and then add them to the ToC and Spine in that order. You could parse these HTML files and fetch
Content of the file html2epub.py
import os
import sys
from ebooklib import epub
if __name__ == '__main__':
book = epub.EpubBook()
# add metadata
book.set_identifier('sample123456')
book.set_title('Sample book')
book.set_language('en')
book.add_author('Author')
all_the_chapters = []
for elem in os.listdir(sys.argv[1]):
# Read html file
content = open(os.path.join(sys.argv[1], elem), 'rt').read()
file_name = os.path.splitext(elem)[0]
# Create new chapter
chapter = epub.EpubHtml(title=file_name, file_name=file_name + '.xhtml')
# Set the content
chapter.content = content
# Add new chapter to the book
book.add_item(chapter)
# Add new chapter to the list of all newly created chapters
all_the_chapters += [chapter]
# Standard stuff
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())
book.toc = all_the_chapters
book.spine = all_the_chapters
# create epub file
epub.write_epub('test.epub', book, {})
Hi I have html file more than one. Each html file corresponds to a chapter. But when I used loops it occurs an issue. "'EpubHtml' object is not iterable". How can I figure it out? Thanks.
My code: ` c1 = epub.EpubHtml(title=article_header, file_name=article_header+'.xhtml', lang='tr') c1.content= text
`