Ebooklib

0.20 · active · verified Sun Apr 12

Ebooklib is a Python library designed to handle EPUB2 and EPUB3 format ebooks. It provides functionalities for reading, writing, and manipulating EPUB files, allowing developers to programmatically create or modify ebook content. The library is actively maintained, with its latest version being 0.20 and a regular release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic EPUB file using Ebooklib. It covers setting book metadata, adding an HTML chapter, defining the table of contents, and writing the final EPUB to disk.

import ebooklib
from ebooklib import epub

# Create a new book
book = epub.EpubBook()

# Set metadata
book.set_identifier('sample123456')
book.set_title('My Awesome Book')
book.set_language('en')
book.add_author('John Doe')

# Add chapter
c1 = epub.EpubHtml(title='Intro', file_name='chap_01.xhtml', lang='en')
c1.content = '<h1>Introduction</h1><p>This is an introduction.</p>'

# Add a stylesheet (optional)
style = 'body { font-family: Calibri,sans-serif; }'
nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style)
book.add_item(nav_css)

book.add_item(c1)

# Define Table Of Contents
book.toc = (epub.Link('chap_01.xhtml', 'Introduction', 'intro'),)

# Add default NCX and Nav file
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())

# Define spine
book.spine = ['nav', c1]

# Write the book
epub.write_epub('my_awesome_book.epub', book, {})

print("Epub book 'my_awesome_book.epub' created successfully!")

view raw JSON →