An enhanced python library for dealing with EPUB2 files. Based on py-clave development release.
As of version 0.4.0, the module works in both python 2.7+ and Python 3.6+
pip install pyepub
This will install the EPUB library in your current python environment as pyepub
.
The code is as documented as I could. First import
the EPUB class to use:
from pyepub import EPUB
And you're pretty much done. Since pyepub.EPUB inherits largely from zipfile.Zipfile, the inferface is quite familiar.
For example, you can create a new EPUB to write into using the "w" flag:
from pyepub import EPUB
epub = EPUB("newfile.epub", "w")
By default the epub is open
-ed in read-only mode and exposes json-able dictionary of OPF properties.
>>> from pyepub import EPUB
>>> epub = EPUB("file.epub")
>>> epub.info
{"metadata":[...], "manifest": [...], "spine": [...], "guide": [...]}
The EPUB can be opened in append ("a") mode, thus enabling adding content.
Due to the internal nature of zipfile stdlib module, a zipfile can't overwrite its contents.
Thusly, a EPUB opened for append is never overwritten. The EPUB.__init__
constructor closes the local file and swaps
the reference with a BytesIO
file-like object. To write the final file to disk, you can call the EPUB.writetodisk()
method:
>>> from pyepub import EPUB
>>> from six import BytesIO
>>> epub = EPUB("file.epub","a")
>>> epub.close() # not necessary, since .writetodisk() will close the file for you.
>>> epub.writetodisk("newfile.epub")
>>> epub.filename # the "file" remains available at .filename property, and can be .read() as usual.
<io.BytesIO instance at 0x1004a8c20>
pyepub is distributed according to the MIT license. I don't like GPL-esque licenses, and I reinvented the wheel (since there already is a EPUB library in pypi) to avoid involving GPL in my projects.