Skip to content

fix: Handle TOC items with uid=None to prevent TypeError when writing EPUB#484

Merged
yihong0618 merged 1 commit intoyihong0618:mainfrom
yusunglee2074:fix-toc-uid-none-error
Jan 7, 2026
Merged

fix: Handle TOC items with uid=None to prevent TypeError when writing EPUB#484
yihong0618 merged 1 commit intoyihong0618:mainfrom
yusunglee2074:fix-toc-uid-none-error

Conversation

@yusunglee2074
Copy link
Contributor

Summary

  • Some EPUB files have TOC (Table of Contents) items where the uid attribute is None
  • When ebooklib tries to write the NCX file, it passes this None value to lxml's SubElement which expects bytes or unicode, causing a TypeError
  • This fix adds a _fix_toc_uids() method that recursively processes the TOC and assigns auto-generated uids (navpoint-0, navpoint-1, etc.) to any items that have uid=None

Error before fix

File "ebooklib/epub.py", line 1356, in _create_section
    np = etree.SubElement(itm, "navPoint", {"id": item.uid})
TypeError: Argument must be bytes or unicode, got 'NoneType'

Test plan

  • Tested with an EPUB file that has uid=None in TOC items
  • Verified that the generated EPUB file is valid and can be opened

🤖 Generated with Claude Code

… EPUB

Some EPUB files have TOC (Table of Contents) items where the uid attribute
is None. When ebooklib tries to write the NCX file, it passes this None
value to lxml's SubElement which expects bytes or unicode, causing a
TypeError.

This fix adds a _fix_toc_uids() method that recursively processes the TOC
and assigns auto-generated uids (navpoint-0, navpoint-1, etc.) to any
items that have uid=None.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@yihong0618
Copy link
Owner

nice

@yihong0618 yihong0618 merged commit 670ca45 into yihong0618:main Jan 7, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants