epublib.ncx.reset module

get_minimal_ncx_content(title: str, lang: str | None) bytes

Get a minimal NCX file content with the given title and language. Caution: the minimality of this template is in regard to the parsing available in this library. To get a minimal valid NCX file, consider using EPUB.generate_ncx instead.

Parameters:
  • title – The title of the NCX document.

  • lang – The language of the NCX document, or None for no language.

generate_ncx(
book: BookProtocol,
filename: str | Path | None = None,
) NCXFile

Generate an NCX file for the given book, based on its metadata and navigation document. The generated NCX file will be added to the book’s resources, and the book’s spine will be updated to reference it.

Typically used from an EPUB instance:

>>> from epublib import EPUB
>>> with EPUB(sample) as book:
...     if not book.ncx:
...         book.generate_ncx()
...     else:
...         book.reset_ncx()
NCXFile(...)
Parameters:
  • book – The book to generate the NCX for.

  • filename – The filename to use for the NCX file in the EPUB archive. If None, “toc.ncx” in the book’s base directory will be used.

Returns:

The generated NCX file.

reset_ncx(
book: BookProtocol,
ncx: NCXFile | None = None,
) NCXFile

Reset the given NCX file to match the book’s metadata and navigation document. If no NCX file is given, the book’s existing NCX file will be used. If the book has no NCX file, a new one will be generated.

Typically used from an EPUB instance: >>> from epublib import EPUB >>> with EPUB(sample) as book: … if not book.ncx: … book.generate_ncx() … else: … book.reset_ncx() NCXFile(…)

Parameters:
  • book – The book to reset the NCX for.

  • ncx – The NCX file to reset, or None to use the book’s existing NCX file, or generate a new one if none exists.