ebooklib is a free, open source publishing project written in Python and released under AGPL-3.0. It has 1,809 GitHub stars, 259 forks and 110 open issues, and was last pushed 2 months ago. On this registry it ranks #25 of 46 tracked projects in Publishing, with 5 head-to-head comparisons available.

What is ebooklib?

What it is

EbookLib is a Python library for EPUB2/EPUB3 manipulation and processing. It reads and writes EPUB files programmatically. It lives in the Python content publishing ecosystem. It solves the problem of handling EPUB2/EPUB3 files in Python code without manually building and reading each component.

The API aims to be simple while allowing complex EPUB work. Version 0.20 was the final Python 2.7 release. Later releases require Python 3.10+, drop six, and ship type annotations. The license is AGPL-3.0. The repository has 1809 stars, 259 forks, 110 open issues, and a 13 year history. Topics are epub, python, and python-library. The homepage is ebooklib.readthedocs.io. The README gives examples.

Key capabilities

  • It reads EPUB files with epub.read_epub and inspects items by type, including images via ITEM_IMAGE.
  • It writes EPUB files with epub.write_epub after building an EpubBook object.
  • It sets core book fields: identifier, title, language, authors, and Dublin Core metadata such as description and publisher.
  • It adds authors with extended fields: file_as, role, and uid.
  • It creates HTML chapters with EpubHtml, including title, file name, language, and content.
  • It adds images and CSS as EPUB items with EpubImage and EpubItem, specifying uid, file_name, media_type, and content.
  • It builds table of contents and spine using toc, Link, Section, and spine; it also adds EpubNcx and EpubNav.

Who uses it and how

  • Booktype uses it for ebook publishing workflows.
  • Audiblez, e2m, ebook2audiobook, Marker, and Telemeta use it in ebook processing or conversion projects.
  • Marker, ChatDev, DocsGPT, ebook2audiobook, and SuperAGI are listed as popular projects using it.
  • Python developers use it in scripts to export all images from an EPUB by iterating image items and writing each file.
  • Developers use it to generate multilingual EPUBs by adding English and German EpubHtml chapters and a sectioned table of contents.

Getting started

Install or use packages mentioned: Debian and Ubuntu provide python-ebooklib; Sphinx docs are generated from docs/ templates and available at ebooklib.readthedocs.io. Typical use is Python code importing ebooklib and epub, then calling read_epub or write_epub.

When to use it — and when not to

Use it when Python code needs direct EPUB2/EPUB3 read/write control, especially for metadata, TOC, spine, images, and CSS. Avoid it if you need a full hosted publishing platform, because it is a library, not an application, and the facts list no database, storage, SMTP, or deployment service. AGPL-3.0, the Python 3.10+ requirement, and 110 open issues are constraints for some integrations.

project readme (upstream, from github) — read inline

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. Starting with the next release EbookLib requires Python 3.10+, has no dependency on six anymore and ships with type annotations.

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.

Frequently asked questions

Is ebooklib free to use?

ebooklib is open source under the AGPL-3.0 licence. There is no licence fee and no seat count — you can self-host it or, where the project offers one, pay a vendor for a managed version instead.

What does ebooklib do?

A versatile Python library for EPUB2/EPUB3 manipulation and processing.

What is ebooklib written in?

ebooklib is primarily written in Python. Its source is publicly available at https://github.com/aerkalov/ebooklib, and it has 1,809 GitHub stars.