Open Source Python Library for 3D Data Processing
Free Python 3D library to create apps for 3D data processing. It allows to generate scenes, surface alignment, 3D visualization & more.
The eBook file format is getting very popular with every passing day. It is short for 'electronic book, and allows users to read it on an electronic device like a computer or handheld device which simplify and enhance the overall learning experience. It has very less cost as compare to the printed books because there is no paper printing.
EbookLib is a very useful open source simple to use Python E-book library that enables software developers to programmatically manage EPUB2, EPUB3 and Kindle Files EPUB files via Free Python. The library is very simple to use and can handle very complex tasks with ease. There are several important tasks part of the library, such as reading EPUB books, creating EPUBs, creating chapters, create image from local image, covers, define table of contents, add styles & formatting, spine, guide, metadata and so on.
Getting Started with EbookLib
EbookLib requires Python 2.7 and Python 3. EbookLib is available at pip, You can easily download it and install it on your machine. Please use the following command for smooth installation.
Install EbookLib via pip
pip install EbookLib
You can download the compiled shared library from Github repository.
How to Generate EPUB Documents via Python?
The open source EbookLib library has provided complete functionality for generating EPUB Documents with just a couple of lines of code. The library has provided several important features related new documents creation and management such as creating intro chapters, adding metadata, defining style of your choice, inserting new chapters, creating table of contents, adding section, adding auto created links to chapters, adding navigation files, creating spine and many more.
How to Create EPUB Files via Python Library?
from ebooklib import epub
book = epub.EpubBook()
# set metadata
book.set_identifier("id123456")
book.set_title("Sample book")
book.set_language("en")
book.add_author("Author Authorowski")
book.add_author(
"Danko Bananko",
file_as="Gospodin Danko Bananko",
role="ill",
uid="coauthor",
)
# create chapter
c1 = epub.EpubHtml(title="Intro", file_name="chap_01.xhtml", lang="hr")
c1.content = (
"Intro heading
"
"Zaba je skocila u baru.
"
'
'
)
# create image from the local image
image_content = open("ebooklib.gif", "rb").read()
img = epub.EpubImage(
uid="image_1",
file_name="static/ebooklib.gif",
media_type="image/gif",
content=image_content,
)
# add chapter
book.add_item(c1)
# add image
book.add_item(img)
# define Table Of Contents
book.toc = (
epub.Link("chap_01.xhtml", "Introduction", "intro"),
(epub.Section("Simple book"), (c1,)),
)
# add default NCX and Nav file
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())
# define CSS style
style = "BODY {color: white;}"
nav_css = epub.EpubItem(
uid="style_nav",
file_name="style/nav.css",
media_type="text/css",
content=style,
)
# add CSS file
book.add_item(nav_css)
# basic spine
book.spine = ["nav", c1]
# write to the file
epub.write_epub("test.epub", book, {})
Read Existing EPUB Documents via Python
The open source EbookLib library gives software developers the ability to open and read existing EPUB2 and EPUB3 files inside their own Python applications. The library has also included complete support for Kindle format (can only read for now). For reading EPUB book you need to pass the full file path to the EPUB file as argument from the ebooklib.epub.read_epub() function. The following example shows how to achieve the task.
How to Read Existing EPUB Documents via Python API?
import ebooklib
from ebooklib import epub
book = epub.read_epub('test.epub')
Manage Items in EPUB Documents via Python
There are various items supported inside a EPUB file such as Style sheets, images, videos, sounds, scripts and html files. The open source EbookLib library has provided complete support for handling items inside EPUB Documents. It supports several important features for handling items such as fetch items by their type, Fetch items by their id, Fetch items by their filename, fetch it by their media type, Return all of the items for handling unknown EPUB files and many more.
Get Items Details from EPUB Documents via Python API
for item in book.get_items():
if item.get_type() == ebooklib.ITEM_DOCUMENT:
print('==================================')
print('NAME : ', item.get_name())
print('----------------------------------')
print(item.get_content())
print('==================================')