MCPcopy Index your code
hub / github.com/aerkalov/ebooklib

github.com/aerkalov/ebooklib @v0.20

repository ↗ · DeepWiki ↗ · release v0.20 ↗ · + Follow
195 symbols 687 edges 28 files 58 documented · 30%
README

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 is the final version supporting Python 2.7. Moving forward, we're leaving legacy code behind and embracing modern Python features to make EbookLib even more awesome.

We are working on refreshing the project so please check and comment if you have your own ideas what needs to happen with the project.

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

Welcome to a journey into the unknown. In these pages, you'll discover "
    "secrets that have remained hidden for centuries.

"
    '

<img alt="Book Cover" src="https://github.com/aerkalov/ebooklib/raw/v0.20/static/ebooklib.gif"/>

'
)
# Create chapter in German
c2 = epub.EpubHtml(title="Einführung", file_name="einfuehrung.xhtml", lang="de")
c2.content = (
    "<h1>Das Buch des Geheimnisvollen</h1>"
    "

Willkommen zu einer Reise ins Unbekannte. Auf diesen Seiten werden Sie "
    "Geheimnisse entdecken, die jahrhundertelang verborgen geblieben sind.

"
)

# 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)

License

EbookLib is licensed under the AGPL license.

Authors

Full list of authors is in AUTHORS.txt file.

Core symbols most depended-on inside this repo

add_item
called by 48
ebooklib/epub.py
leave_only
called by 45
ebooklib/plugins/standard.py
add_metadata
called by 16
ebooklib/epub.py
debug
called by 15
ebooklib/utils.py
parse_html_string
called by 15
ebooklib/utils.py
read
called by 14
ebooklib/utils.py
get_name
called by 13
ebooklib/epub.py
add_item
called by 13
ebooklib/epub.py

Shape

Method 153
Class 28
Function 14

Languages

Python100%

Modules by API surface

ebooklib/epub.py113 symbols
tests/test_ebook.py23 symbols
ebooklib/utils.py12 symbols
ebooklib/plugins/base.py9 symbols
tests/test_epub_item.py6 symbols
tests/test_epub_html.py6 symbols
ebooklib/plugins/booktype.py6 symbols
ebooklib/plugins/tidyhtml.py5 symbols
tests/test_utils.py3 symbols
ebooklib/plugins/standard.py3 symbols
ebooklib/plugins/sourcecode.py3 symbols
tests/test_epub_cover.py2 symbols

For agents

$ claude mcp add ebooklib \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact