admin管理员组

文章数量:1434978

Objective

To programmatically create a epub file from text files

Problem

Some of the sub chapters are not shown

Minimal Reproducible Example

from ebooklib import epub

# Create a new EPUB book
book = epub.EpubBook()

# Set metadata
book.set_identifier('id123456')
book.set_title('book1')
book.set_language('en')
book.add_author('John Doe')

# Create a single chapter that includes all content
combined_chapter1 = epub.EpubHtml(title='Chaptor1', file_name='chapters1.xhtml', lang='en')

# Add content with main chapters, sub-chapters, and sub-sub-chapters
combined_chapter1.content = '''
<h1 id="chapter1">Chapter 1: Main Topic</h1>
<p>Introduction to Chapter 1.</p>

<h2 id="chapter1.1">1.1 Sub-Chapter</h2>
<p>Content of sub-chapter 1.1.</p>

<h3 id="chapter1.1.1">1.1.1 Sub-Sub-Chapter</h3>
<p>Detailed content of sub-sub-chapter 1.1.1.</p>

<h2 id="chapter1.2">1.2 Sub-Chapter</h2>
<p>Content of sub-chapter 1.2.</p>

<h3 id="chapter1.2.1">1.2.1 Sub-Sub-Chapter</h3>
<p>Detailed content of sub-sub-chapter 1.2.1.</p>
'''

# Add the combined chapter to the book
book.add_item(combined_chapter1)

# Define Table of Contents with links to all sections
book.toc = (
    epub.Link('chapters1.xhtml#chapter1', 'Chapter 1: Main Topic', 'chapter1'),
    (
        epub.Link('chapters1.xhtml#chapter1.1', '1.1 Sub-Chapter', 'chapter1.1'),
        (
            (epub.Link('chapters1.xhtml#chapter1.1.1', '1.1.1 Sub-Sub-Chapter', 'chapter1.1.1'),)
        ),
        epub.Link('chapters1.xhtml#chapter1.2', '1.2 Sub-Chapter', 'chapter1.2'),
        (
            (epub.Link('chapters1.xhtml#chapter1.2.1', '1.2.1 Sub-Sub-Chapter', 'chapter1.2.1'),)
        ),
    ),
)

# Add navigation files
book.add_item(epub.EpubNcx())
nav = epub.EpubNav()
book.add_item(nav)

# Set the spine
book.spine = ['nav', combined_chapter1]

# Write the EPUB file
epub.write_epub('my_book_ch1_ch2.epub', book, {})

What the Result Looks Like


Calibre was used. 1.2 Sub-Chapter and 1.2.1 Sub-Sub-Chapter are missing. They should be on the table of contents.

What I tried so far

I googled "ebooklib sub chapter not shown" and checked the first 10 pages in vain.

Environment

  • Windows 10
  • VSCode 1.95.3
  • python 3.12.4

Any assistance would be appreciated.

Objective

To programmatically create a epub file from text files

Problem

Some of the sub chapters are not shown

Minimal Reproducible Example

from ebooklib import epub

# Create a new EPUB book
book = epub.EpubBook()

# Set metadata
book.set_identifier('id123456')
book.set_title('book1')
book.set_language('en')
book.add_author('John Doe')

# Create a single chapter that includes all content
combined_chapter1 = epub.EpubHtml(title='Chaptor1', file_name='chapters1.xhtml', lang='en')

# Add content with main chapters, sub-chapters, and sub-sub-chapters
combined_chapter1.content = '''
<h1 id="chapter1">Chapter 1: Main Topic</h1>
<p>Introduction to Chapter 1.</p>

<h2 id="chapter1.1">1.1 Sub-Chapter</h2>
<p>Content of sub-chapter 1.1.</p>

<h3 id="chapter1.1.1">1.1.1 Sub-Sub-Chapter</h3>
<p>Detailed content of sub-sub-chapter 1.1.1.</p>

<h2 id="chapter1.2">1.2 Sub-Chapter</h2>
<p>Content of sub-chapter 1.2.</p>

<h3 id="chapter1.2.1">1.2.1 Sub-Sub-Chapter</h3>
<p>Detailed content of sub-sub-chapter 1.2.1.</p>
'''

# Add the combined chapter to the book
book.add_item(combined_chapter1)

# Define Table of Contents with links to all sections
book.toc = (
    epub.Link('chapters1.xhtml#chapter1', 'Chapter 1: Main Topic', 'chapter1'),
    (
        epub.Link('chapters1.xhtml#chapter1.1', '1.1 Sub-Chapter', 'chapter1.1'),
        (
            (epub.Link('chapters1.xhtml#chapter1.1.1', '1.1.1 Sub-Sub-Chapter', 'chapter1.1.1'),)
        ),
        epub.Link('chapters1.xhtml#chapter1.2', '1.2 Sub-Chapter', 'chapter1.2'),
        (
            (epub.Link('chapters1.xhtml#chapter1.2.1', '1.2.1 Sub-Sub-Chapter', 'chapter1.2.1'),)
        ),
    ),
)

# Add navigation files
book.add_item(epub.EpubNcx())
nav = epub.EpubNav()
book.add_item(nav)

# Set the spine
book.spine = ['nav', combined_chapter1]

# Write the EPUB file
epub.write_epub('my_book_ch1_ch2.epub', book, {})

What the Result Looks Like


Calibre was used. 1.2 Sub-Chapter and 1.2.1 Sub-Sub-Chapter are missing. They should be on the table of contents.

What I tried so far

I googled "ebooklib sub chapter not shown" and checked the first 10 pages in vain.

Environment

  • Windows 10
  • VSCode 1.95.3
  • python 3.12.4

Any assistance would be appreciated.

Share Improve this question edited Nov 17, 2024 at 22:08 dixhom asked Nov 17, 2024 at 13:26 dixhomdixhom 3,0534 gold badges23 silver badges39 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The solution I use is section objects in place of parent links. From the Section object's source code:

class Section(object):

    def __init__(self, title, href=''):
        self.title = title
        self.href = href

By using href, we turn a plain section title into a working link. We then update your table of contents:

book.toc = (
    (epub.Section('Chapter 1: Main Topic', 'chapters1.xhtml#chapter1'),
    (
        (epub.Section('1.1 Sub-Chapter', 'chapters1.xhtml#chapter1.1'),
            (epub.Link('chapters1.xhtml#chapter1.1.1', '1.1.1 Sub-Sub-Chapter', 'chapter1.1.1'),)
        ),
        (epub.Section('1.2 Sub-Chapter', 'chapters1.xhtml#chapter1.2'),
            (epub.Link('chapters1.xhtml#chapter1.2.1', '1.2.1 Sub-Sub-Chapter', 'chapter1.1.1'),)
        ),
    )),
)

You can still use regular link objects as section headers if you prefer. Using this solution as reference:

book.toc = (
    (epub.Link('chapters1.xhtml#chapter1', 'Chapter 1: Main Topic', 'chapter1'),
    (
        (epub.Link('chapters1.xhtml#chapter1.1', '1.1 Sub-Chapter', 'chapter1.1'),
            (epub.Link('chapters1.xhtml#chapter1.1.1', '1.1.1 Sub-Sub-Chapter', 'chapter1.1.1'),)
        ),
        (epub.Link('chapters1.xhtml#chapter1.2', '1.2 Sub-Chapter', 'chapter1.2'),
            (epub.Link('chapters1.xhtml#chapter1.2.1', '1.2.1 Sub-Sub-Chapter', 'chapter1.1.1'),)
        ),
    )),
)

In both cases, each chapter is embedded in a tuple rather than sitting directly in the table of contents.

本文标签: windowssub chapters not shown in ebooklib in pythonStack Overflow