Search code examples
pythondjangoepub

how to create a link which points to the a potion of a chapter by using ebooklib python


I developing an application in DJango python to create EPub fies dynamically. I am using CKEditor and ebooklib. But I have a doubt with TOC, My intention is to create a link in TOC which points to the a potion of a chapter. We can create the same as section and link to chapter but when i create so section load as a separate page,see its sample code below

Please see the sample code

 c2 = epub.EpubHtml(title='About this book', file_name='about.xhtml')
    c2.content='<h1>About this book</h1><p>Helou, this is my book! There are many books, but this one is mine.</p>'
    c2.set_language('hr')
    c2.properties.append('rendition:layout-pre-paginated rendition:orientation-landscape rendition:spread-none')
    c2.add_item(default_css)

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

  
    # create table of contents
    # - add manual link
    # - add section
    # - add auto created links to chapters

    book.toc = (epub.Link('intro.xhtml', 'Introduction', 'intro'),
                (epub.Section('Languages'),
                 (c1, c2))
                )

But when I execute I will get section in a separate page.My intention is to point to the section of the chapter, not as a separate page. (its like pointing to a subheading in chapter) and add section link in TOC (table of contents). Please help ?

Thanks


Solution

  • I don't know if you solved your problem or not, but I had the same issue and after a little testing and trying this and that I found a solution.

    The SOLUTION is to add the chapters you want as a epub.Link object. As an example:

    epub.Link(chapter.file_name, chapter.title, chapter.id)
    

    And add that result to a toc list object.

    toc.append(link)
    

    Then after you have added all the chapters / links you want, then you convert the list to a tuple.

    toc = tuple(toc)
    

    And set it as the book table of contents.

    book.toc = toc