Skip to main content

About EbookLib

EbookLib is a Python library for managing EPUB2/EPUB3. It's capable of reading and writing EPUB files programmatically.

Heads up! EbookLib 0.20 was the final version supporting Python 2.7. Since EbookLib 0.21 the library requires Python 3.10+, has no dependency on six anymore and ships with type annotations. write_epub() now raises exceptions by default; see CHANGES.txt for the upgrade notes.

Want to contribute? It's easy! We welcome bug reports, suggestions, and Pull Requests. Before submitting a Pull Request, please take a quick look at our simple guidelines. Our contribution process is straightforward, with no special procedures to worry about.

The API is designed to be as simple as possible, while at the same time making complex things possible too. It has support for covers, table of contents, spine, guide, metadata and etc.

EbookLib is used in Booktype from Sourcefabric, as well as Audiblez, e2m, ebook2audiobook, Marker and Telemeta. You can find a more extensive list of projects utilizing EbookLib here.

Packages of EbookLib for GNU/Linux are available in Debian and Ubuntu.

Sphinx documentation is generated from the templates in the docs/ directory and made available at http://ebooklib.readthedocs.io

Usage

Reading

import os
import ebooklib
from ebooklib import epub

book = epub.read_epub('test.epub')

# Export all images from the Book
for image in book.get_items_of_type(ebooklib.ITEM_IMAGE):
    with open(os.path.basename(image.get_name()), "wb") as f:
        f.write(image.get_content())

Writing

from ebooklib import epub

book = epub.EpubBook()

# Set metadata
book.set_identifier("GB33BUKB20201555555555")
book.set_title("The Book of the Mysterious")
book.set_language("en")

book.add_author("John Smith")
book.add_author(
    "Hans Müller",
    file_as="Dr. Hans Müller",
    role="ill",
    uid="coauthor",
)

book.add_metadata("DC", "description", "A mysterious journey into hidden secrets")
book.add_metadata("DC", "publisher", "Mystic Books Publishing House")

# Create chapter in English
c1 = epub.EpubHtml(title="Introduction", file_name="introduction.xhtml", lang="en")
c1.content = (
    "<h1>The Book of the Mysterious</h1>"
    "<p>Welcome to a journey into the unknown. In these pages, you'll discover "
    "secrets that have remained hidden for centuries.</p>"
    '<p><img alt="Book Cover" src="static/ebooklib.gif"/></p>'
)
# Create chapter in German
c2 = epub.EpubHtml(title="Einführung", file_name="einfuehrung.xhtml", lang="de")
c2.content = (
    "<h1>Das Buch des Geheimnisvollen</h1>"
    "<p>Willkommen zu einer Reise ins Unbekannte. Auf diesen Seiten werden Sie "
    "Geheimnisse entdecken, die jahrhundertelang verborgen geblieben sind.</p>"
)

# Create image from the local image
img = epub.EpubImage(
    uid="image_1",
    file_name="static/ebooklib.gif",
    media_type="image/gif",
    content=open("ebooklib.gif", "rb").read(),
)

# Define CSS style
nav_css = epub.EpubItem(
    uid="style_nav",
    file_name="style/nav.css",
    media_type="text/css",
    content="BODY {color: black; background-color: white;}",
)

# Every chapter must me added to the book
book.add_item(c1)
book.add_item(c2)
# This also includes images, style sheets, etc.
book.add_item(img)
book.add_item(nav_css)

# Define Table Of Contents
book.toc = (
    epub.Link("introduction.xhtml", "Introduction", "intro"),
    (epub.Section("Deutsche Sektion"), (c2,)),
)

# Basic spine
book.spine = ["nav", c1, c2]

# Add default NCX (not required) and Nav files.
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())

# Write to the file
epub.write_epub("the_book_of_the_mysterious.epub", book)

Popular Projects using Ebooklib

  1. Marker
  2. ChatDev
  3. DocsGPT
  4. ebook2audiobook
  5. SuperAGI
  6. bilingual_book_maker
  7. audiblz

License

EbookLib is licensed under the AGPL license.

Authors

Full list of authors is in AUTHORS.txt file.

Metadata

Release files for EbookLib 0.21

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

Source distribution (sdist)

Source distribution for EbookLib 0.21
File Size Uploaded Provenance Actions
ebooklib-0.21.tar.gz 148.7 kB Without provenance Details for ebooklib-0.21.tar.gz

Built distribution (wheel)

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

Total release size: 201.2 kB

Release files / ebooklib-0.21.tar.gz

Download URL ebooklib-0.21.tar.gz
Size 148.7 kB
Tags Source
SHA-256 checksum
How to use checksums
fa815d544f722321c9fd31d8957473b2617027d3091431aa690d6dd93f785222
BLAKE2b-256 checksum
How to use checksums
fcd8a7ec6e06014057918bd58d1495af2e9c4e21cf14c93d10c625e905a8285d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.16

Release files / ebooklib-0.21-py3-none-any.whl

Download URL ebooklib-0.21-py3-none-any.whl
Size 52.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8cb417bed66567210661e6ef203f6310f394ad4a282f385a07320cbc09906c30
BLAKE2b-256 checksum
How to use checksums
15725dde31a7b988f13ea158936e10bf82e7f6c1763201b325774ea93a0cf8ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.16

Security for EbookLib 0.21

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