Skip to main content

epubfile

import epubfile
book = epubfile.Epub('mybook.epub')

for text_id in book.get_texts():
    soup = book.read_file(text_id, soup=True)
    ...
    book.write_file(text_id, soup)

for image_id in book.get_images():
    data = book.read_file(image_id)
    ...
    book.write_file(image_id, data)

# Note that this only reverses the spine in the opf file, it does not reverse
# the table of contents, since the toc is stored as nav.xhtml and toc.ncx.
book.set_spine_order(reversed(book.get_spine_order()))

cover_id = book.get_cover_image()
if cover_id:
    book.rename_file(cover_id, 'myfavoritecoverimage')

book.save('modifiedbook.epub')

epubfile provides simple editing of epub books. epubfile attempts to keep file modifications to a minimum. It does not add, remove, or rearrange files unless you ask it to, and does not inject additional metadata. As such, it works for both epub2 and epub3 assuming you stick to supported operations for your book version.

Install

pip install epubfile

Command line utilities

This library was born out of my own needs. So there are a couple of builtin utilities. You can run epubfile.py with no arguments to see a summary of all builtin utilities. Here are some examples:

addfile:
    Add files into the book.

covercomesfirst:
    Rename the cover image file so that it is the alphabetically-first image.

merge:
    Merge multiple books into one.

normalize:
    Rename files and directories in the book to match a common structure.

To see the rest, try epubfile --help.

Spec compliance

epubfile does not rigorously enforce the epub spec and you can create noncompliant books with it. Basic errors are checked, and I am open to issues and comments regarding ways to improve spec-compliance without adding significant size or complexity to the library. I am prioritizing simplicity and ease of use over perfection.

Pairs well with...

For advanced inter-file operations and better validation, I suggest using this library in conjunction with a good editor like Sigil. I wrote this library because although Sigil plugins are great for processing a single book, it is difficult to use Sigil to process multiple books, read book data for use in other programs, or do other inter-book operations.

What not to expect

I do not intend to implement an object model for book metadata, beyond perhaps some basic getters and setters. You have full control over the Epub.opf BeautifulSoup object so you can edit the metadata however you want.


Mirrors

https://git.voussoir.net/voussoir/epubfile

https://github.com/voussoir/epubfile

https://gitlab.com/voussoir/epubfile

https://codeberg.org/voussoir/epubfile

Metadata

Release files for epubfile 0.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for epubfile 0.0.9
File Size Uploaded
epubfile-0.0.9.tar.gz 20.3 kB Details

Release files / epubfile-0.0.9.tar.gz

Download URL epubfile-0.0.9.tar.gz
Size 20.3 kB
Tags Source
SHA-256 checksum
How to use checksums
74463dfc9ed75ede50fad741f34f7ba7bc12f81600397407615f51dce224d6c0
BLAKE2b-256 checksum
How to use checksums
2dd62fd551fa5a2172bc980a67d200abb72b44146f9b5df92b330cc9a020fcd5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.31.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6

Release history Release notifications | RSS feed

This release

0.0.9 This release

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page