Skip to main content

A Python package for parsing chapters from EPUBs.

Project description

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" and "sentences" 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

Example output

For dracula.epub the returned chapters list looks like:

[
    {
        "title": "Jonathan Harker's Journal",
        "sentences": [
            "3 May. Bistritz.—Left Munich at 8:35 P. M., on 1st May, arriving at Vienna early next morning; should have arrived at 6:46, but train was an hour late.",
            "Buda-Pesth seems a wonderful place, from the glimpse which I got of it from the train and the little I could walk through the streets.",
            "I feared to go very far from the station, as we had arrived late and would start as near the correct time as possible.",
            "The impression I had was that we were leaving the West and entering the East; the most western of splendid bridges over the Danube, which is here of noble width and depth, took us among the traditions of Turkish rule."
        ]
    },
    {
        "title": "Jonathan Harker's Journal (Continued)",
        "sentences": [
            "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.",
            "In the gloom the courtyard looked of considerable size, and as several dark ways led from it under great round arches, it perhaps seemed bigger than it really is.",
            ...
        ]
    },
    ...
]

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

epubchapterize-0.2.0.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

epubchapterize-0.2.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file epubchapterize-0.2.0.tar.gz.

File metadata

  • Download URL: epubchapterize-0.2.0.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for epubchapterize-0.2.0.tar.gz
Algorithm Hash digest
SHA256 aae557e7ad7bcb09d40732c9d570d4e1a9e35715365d8256acdabaed3ba0705c
MD5 56bb8d95716bc9272353d7668599cefb
BLAKE2b-256 0c4781fd8fd2ed621fac1b5fa032601e2605778d3b254f392185a8646782b86b

See more details on using hashes here.

File details

Details for the file epubchapterize-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: epubchapterize-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for epubchapterize-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9c456a32b7b5d6366a64d868b2b227c92d6120eaf9ab892c208f6b20cf4067da
MD5 8b3cabab11b1ea98da80c7fa4deae34b
BLAKE2b-256 b657502b0618762ef1de95688aa1165b91c6c78070ce66385f5670b728ccea06

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page