{"id":4351,"library":"ebooklib","title":"Ebooklib","description":"Ebooklib is a Python library designed to handle EPUB2 and EPUB3 format ebooks. It provides functionalities for reading, writing, and manipulating EPUB files, allowing developers to programmatically create or modify ebook content. The library is actively maintained, with its latest version being 0.20 and a regular release cadence.","status":"active","version":"0.20","language":"en","source_language":"en","source_url":"https://github.com/aerkalov/ebooklib","tags":["ebook","epub","parser","generator","reader","writer"],"install":[{"cmd":"pip install ebooklib","lang":"bash","label":"Install Ebooklib"}],"dependencies":[{"reason":"Required for parsing and generating XML (OPF, NCX, etc.) within EPUB files.","package":"lxml","optional":false},{"reason":"Used for parsing HTML content within EPUB files.","package":"html5lib","optional":false}],"imports":[{"note":"Primary class for representing an EPUB book.","symbol":"EpubBook","correct":"from ebooklib import epub"},{"note":"Represents an HTML chapter or content document within the EPUB.","symbol":"EpubHtml","correct":"from ebooklib import epub"},{"note":"Function to read an existing EPUB file.","symbol":"read_epub","correct":"from ebooklib import epub"},{"note":"Function to write an EpubBook object to a file.","symbol":"write_epub","correct":"from ebooklib import epub"}],"quickstart":{"code":"import ebooklib\nfrom ebooklib import epub\n\n# Create a new book\nbook = epub.EpubBook()\n\n# Set metadata\nbook.set_identifier('sample123456')\nbook.set_title('My Awesome Book')\nbook.set_language('en')\nbook.add_author('John Doe')\n\n# Add chapter\nc1 = epub.EpubHtml(title='Intro', file_name='chap_01.xhtml', lang='en')\nc1.content = '<h1>Introduction</h1><p>This is an introduction.</p>'\n\n# Add a stylesheet (optional)\nstyle = 'body { font-family: Calibri,sans-serif; }'\nnav_css = epub.EpubItem(uid=\"style_nav\", file_name=\"style/nav.css\", media_type=\"text/css\", content=style)\nbook.add_item(nav_css)\n\nbook.add_item(c1)\n\n# Define Table Of Contents\nbook.toc = (epub.Link('chap_01.xhtml', 'Introduction', 'intro'),)\n\n# Add default NCX and Nav file\nbook.add_item(epub.EpubNcx())\nbook.add_item(epub.EpubNav())\n\n# Define spine\nbook.spine = ['nav', c1]\n\n# Write the book\nepub.write_epub('my_awesome_book.epub', book, {})\n\nprint(\"Epub book 'my_awesome_book.epub' created successfully!\")","lang":"python","description":"This quickstart demonstrates how to create a basic EPUB file using Ebooklib. It covers setting book metadata, adding an HTML chapter, defining the table of contents, and writing the final EPUB to disk."},"warnings":[{"fix":"If your application relies on string content, decode the returned bytes, e.g., `content.decode('utf-8')`.","message":"As of v0.20, `EpubHtml.get_body_content()` now returns bytes instead of a string. Code expecting a string will need to explicitly decode the result.","severity":"breaking","affected_versions":"0.20 and later"},{"fix":"Explicitly set error handling options when calling `write_epub` if you rely on strict validation or specific error reporting behavior. Consult the `write_epub` documentation for available parameters.","message":"The default behavior for error throwing during EPUB writing (via `write_epub`) might have changed in v0.19. Previously, certain errors might have halted the process, but now they might be suppressed or optional.","severity":"gotcha","affected_versions":"0.19 and later"},{"fix":"Migrate new projects to Python 3.6 or newer. If maintaining legacy code, be aware of Python 2 specific issues and limitations.","message":"While Ebooklib officially supports Python 2.7, Python 2 is end-of-life and no longer maintained. Using Ebooklib in new projects should target Python 3.6+ for security, modern features, and better compatibility with the ecosystem.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure all internal links and resource paths are correctly defined and relative to the `file_name` of the `EpubHtml` item. Update to version `0.19` or `0.20` to benefit from path resolution fixes.","message":"Older versions (particularly `0.16` to `0.18`) had known issues with correctly resolving relative paths within EPUB content (e.g., links to images, CSS files). Incorrect relative paths can lead to broken or improperly rendered EPUBs.","severity":"gotcha","affected_versions":"0.16 to 0.18"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}