Rust-powered EPUB2/EPUB3 library for Python. Fast reading, writing, validation, and markdown conversion and a neat MIT license.
Installation
pip install fast-ebook
Quick Start
Reading an EPUB
from fast_ebook import epub import fast_ebook book = epub . read_epub ( 'book.epub' ) # Metadata print ( book . get_metadata ( 'DC' , 'title' )) print ( book . get_metadata ( 'DC' , 'creator' )) # Iterate items for item in book . get_items (): print ( item . get_id (), item . get_name (), item . get_type ()) # Filter by type for img in book . get_items_of_type ( fast_ebook . ITEM_IMAGE ): print ( img . get_name (), len ( img . get_content ()), 'bytes' ) # Lookup by ID or href item = book . get_item_with_id ( 'chapter1' ) item = book . get_item_with_href ( 'text/chapter1.xhtml' ) # Table of contents for entry in book . toc : print ( entry . title , entry . href )
Writing an EPUB
from fast_ebook import epub book = epub . EpubBook () book . set_identifier ( 'id123' ) book . set_title ( 'My Book' ) book . set_language ( 'en' ) book . add_author ( 'Author Name' ) c1 = epub . EpubHtml ( title = 'Intro' , file_name = 'chap_01.xhtml' , lang = 'en' ) c1 . content = '<h1>Hello</h1><p>World</p>' book . add_item ( c1 ) book . add_item ( epub . EpubNcx ()) book . add_item ( epub . EpubNav ()) book . toc = [ epub . Link ( 'chap_01.xhtml' , 'Introduction' , 'intro' )] book . spine = [ 'nav' , c1 ] epub . write_epub ( 'output.epub' , book )
Reading from / writing to BytesIO
import io from fast_ebook import epub # Read from bytes with open ( 'book.epub' , 'rb' ) as f : book = epub . read_epub ( f ) # Write to BytesIO buf = io . BytesIO () epub . write_epub ( buf , book ) epub_bytes = buf . getvalue () # Read from raw bytes book = epub . read_epub ( epub_bytes )
... continue reading