Search code examples
pythonebooklib

using ebooklib on Python


I am trying to open an epub file using ebooklib:

pip install ebooklib
import ebooklib
from ebooklib import epub
book = epub.read_epub(epub_path)

The above codes are executed, may I know what command to enter next if I want to open the epub file? Thanks a lot.


Solution

  • book = epub.read_epub(epub_path)
    

    The above code already opens the epub file. If you want to do something like get content from the epub file you can follow instructions at the ebooklib docs located here:

    https://docs.sourcefabric.org/projects/ebooklib/en/latest/tutorial.html#reading-epub

    For example, if you were wanting to just get the documents (the .xhtml files that the epub consists of) you use the following code:

    import ebooklib
    from ebooklib import epub
    
    epub_path = '[YOUR EPUB PATH]'
    
    book = epub.read_epub(epub_path)
    
    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('==================================')
    

    You can edit the above code to get different sorts of content out of the epub file.

    Additionally you could look into what read_epub() is doing by looking at the source code here: https://github.com/aerkalov/ebooklib/blob/master/ebooklib/epub.py