-
Notifications
You must be signed in to change notification settings - Fork 234
Home
Aleksandar Erkalović edited this page May 12, 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.
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')
# 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)
# 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
c1.set_language('hr')
# set properties for this file
c1.properties.append('rendition:layout-pre-paginated rendition:orientation-landscape rendition:spread-none')
# this chapter should also include this css file
c1.add_item(default_css)
book.add_item(c1)
# create table of contents
book.toc = (epub.Link('intro.xhtml', 'Introduction', 'intro'),
(epub.Section('Languages'),
(c1, ))
)
# 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)
# add navigation files
book.add_item(epub.EpubNcx())
nav = epub.EpubNav()
nav.add_item(nav_css)
book.add_item(nav)
# add css files
book.add_item(default_css)
book.add_item(nav_css)
# create spine
book.spine = ['nav', c1]
# create epub file
epub.write_epub('test.epub', book)
EbookLib is licensed under the AGPL license.