Skip to content
Aleksandar Erkalović edited this page May 7, 2013 · 35 revisions

EbookLib is a Python library for managing EPUB2/EPUB3 and Kindle files. It's capable of reading and writing EPUB files programmatically (Kindle support is under development).

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

Sample code

from ebooklib import epub

book = epub.EpubBook()

# add metadata
book.set_identifier('sample123456')
book.set_title('Sample book')
book.set_language('en')

book.add_author('Aleksandar Erkalovic')
book.add_author('Danko Bananko', file_as='Gospodin Danko Bananko', role='ill', uid='coauthor')

# intro chapter
c1 = epub.EpubHtml(title='Introduction', file_name='intro.xhtml')
c1.content=u'<h1>Introduction</h1><p>Introduction paragraph.</p>    
# set language just for this chapter
c2.set_language('hr')
# set properties for this file
c2.properties.append('rendition:layout-pre-paginated rendition:orientation-landscape rendition:spread-none')
# this chapter should also include this css file
c2.add_link(href="style/default.css", rel="stylesheet", type="text/css")

book.add_item(c1)

# create table of contents
book.toc = (epub.Link('intro.xhtml', 'Introduction', 'intro'),
             (epub.Section('Languages'),
             (c1, ))
            )

# add navigation files
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())

# style for navigation file
style = 'BODY { color: black; }
nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style)
book.add_item(nav_css)

# style for chapter
style = '''BODY { text-align: justify;}'''
default_css = epub.EpubItem(uid="style_default", file_name="style/default.css", media_type="text/css", content=style)
book.add_item(default_css)

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

# create epub file
epub.writeEPUB('test.epub', book, {})

Documentation

Development

License

EbookLib is licensed under the AGPL license.

Clone this wiki locally