aerkalov/ebooklib

'EpubHtml' object is not iterable

ehafizoglu opened this issue · 3 comments

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

    # add chapters to the book
    book.add_item(c1)

    # create table of contents
    book.toc = (c1)

    # add navigation files
    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())

    # define css style
    style = '''
            @namespace epub "http://www.idpf.org/2007/ops";
            body {
                font-family: Cambria, Liberation Serif, Bitstream Vera Serif, Georgia, Times, Times New Roman, serif;
            }
            h2 {
                text-align: left;
                text-transform: uppercase;
                font-weight: 200;     
            }
            ol {
                    list-style-type: none;
            }
            ol > li:first-child {
                    margin-top: 0.3em;
            }
            nav[epub|type~='toc'] > ol > li > ol  {
                list-style-type:square;
            }
            nav[epub|type~='toc'] > ol > li > ol > li {
                    margin-top: 0.3em;
            }
            '''

    # add css file
    nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style)
    book.add_item(nav_css)

    # create spine
    book.spine = ['nav', c1]

    if i==1:
        break
    # create epub file
epub.write_epub(prefix+find_month+'.epub', book, {})

`

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 <title> to set up the Title of each chapter. You can add cover and CSS.... But this is the basic idea behind this.

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, {})