aerkalov / ebooklib

Python E-book library for handling books in EPUB2/EPUB3 format -
https://ebooklib.readthedocs.io/
GNU Affero General Public License v3.0
1.42k stars 223 forks source link

'EpubHtml' object is not iterable #259

Open ehafizoglu opened 1 year ago

ehafizoglu commented 1 year ago

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

`

buptyyf commented 1 year ago

I have the same question. Have you solved that?

ehafizoglu commented 1 year ago

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.

aerkalov commented 1 year ago

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 to set up the Title of each chapter. You can add cover and CSS.... But this is the basic idea behind this.</p> <p>Content of the file html2epub.py</p> <pre><code>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, {})</code></pre> </div> </div> <div class="page-bar-simple"> </div> <div class="footer"> <ul class="body"> <li>© <script> document.write(new Date().getFullYear()) </script> Githubissues.</li> <li>Githubissues is a development platform for aggregating issues.</li> </ul> </div> <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script> <script src="/githubissues/assets/js.js"></script> <script src="/githubissues/assets/markdown.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/highlight.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/go.min.js"></script> <script> hljs.highlightAll(); </script> </body> </html>