Skip to main content

EpubChapterize

EpubChapterize is a Python package designed to split EPUB files into chapters programmatically. It provides a simple interface to process EPUB files and extract their chapters for further use. Currently optimized for Project Gutenberg EPUB3s — if it doesn't work for your use case please get in touch.

Installation

pip install epubchapterize

Usage

from epub_chapterize import chapterize

chapters, language, title, author, cover_image = chapterize("dracula.epub")

print(title)    # "Dracula"
print(author)   # "Bram Stoker"
print(language) # "en"

for chapter in chapters:
    print(chapter["title"])
    for sentence in chapter["sentences"]:
        print(sentence)

Return values

Value Type Description
chapters list[dict] List of chapter dicts, each with "title", "sentences", "start_file", "start_id" and "sentence_files" keys
language str BCP 47 language code detected from the EPUB metadata (e.g. "en", "fr")
title str Book title from EPUB metadata
author str Author name from EPUB metadata
cover_image bytes | None Raw bytes of the cover image, or None if not found

A chapter starts in the file "start_file", at the element whose id is "start_id", which is where the table of contents links to (None when it links to the file itself). When headings in the same file with no text of their own lead into a chapter, like a part heading just before its first chapter, the chapter's title starts with theirs and the chapter starts where the first of them does. "sentence_files" lists the files its sentences are in, in order, each with the index in "sentences" of the first sentence there; the sentences up to the next entry's are in that file. Files are paths from the root of the EPUB written as URLs, which is how readers such as Readium name them.

Example output

For the Standard Ebooks dracula.epub the returned chapters list looks like:

[
    {"title": "Imprint", ...},
    {
        "title": "I",
        "sentences": [
            "Jonathan Harker’s Journal.",
            "(Kept in shorthand.)",
            "3 May.",
            ...
        ],
        "start_file": "epub/text/chapter-1.xhtml",
        "start_id": None,
        "sentence_files": [{"file": "epub/text/chapter-1.xhtml", "first_sentence": 0}]
    },
    {
        "title": "II",
        "sentences": [
            "Jonathan Harker’s Journal—continued",
            "5 May.—I must have been asleep, for certainly if I had been fully awake I must have noticed the approach of such a remarkable place.",
            ...
        ],
        "start_file": "epub/text/chapter-2.xhtml",
        "start_id": None,
        "sentence_files": [{"file": "epub/text/chapter-2.xhtml", "first_sentence": 0}]
    },
    ...
]

The cover_image bytes can be written straight to a file:

if cover_image:
    with open("cover.jpg", "wb") as f:
        f.write(cover_image)

Supported languages

Sentence segmentation is supported for English, French, German, Spanish, Italian, Dutch, and Portuguese via NLTK's Punkt tokenizer (installed automatically). No extra downloads are required unless you switch to the spaCy backend.

Requirements

  • Python 3.13 or higher

License

MIT — see the LICENSE file for details.

Contributing

Contributions are welcome! Feel free to submit issues or pull requests at github.com/nzmattgrant/epubchapterize.

Author

Matthew Grant

Metadata

Release files for epubchapterize 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for epubchapterize 0.4.0
File Size Uploaded Provenance Actions
epubchapterize-0.4.0.tar.gz 18.7 kB Without provenance Details for epubchapterize-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for epubchapterize 0.4.0
File Interpreter ABI Platform Actions
epubchapterize-0.4.0-py3-none-any.whl Python 3 none any Details for epubchapterize-0.4.0-py3-none-any.whl

Total release size: 29.6 kB

Release files / epubchapterize-0.4.0.tar.gz

Download URL epubchapterize-0.4.0.tar.gz
Size 18.7 kB
Tags Source
SHA-256 checksum
How to use checksums
8e8a042d04bb83e9808625342d4720dd846cc0dd5f39eb6bffe01767bcecdb7f
BLAKE2b-256 checksum
How to use checksums
d43c8eb3190afaa0d68268a23d05066fb9731cf3d250e0356acf2f20c41c13e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release files / epubchapterize-0.4.0-py3-none-any.whl

Download URL epubchapterize-0.4.0-py3-none-any.whl
Size 10.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c96bdebaa11295703391b3b69c4bf93b26d07d951e5b6e8d872e96311cd48db0
BLAKE2b-256 checksum
How to use checksums
a1b8724817a83f66f83412dce955b33b6f814723a18d8a5ca2f418d64bf7d7a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Security for epubchapterize 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page