aerkalov / ebooklib

Python E-book library for handling books in EPUB2/EPUB3 format -
https://ebooklib.readthedocs.io/
GNU Affero General Public License v3.0
1.49k stars 234 forks source link

Write ebook file directly into db instead file #306

Open jaksatomovic opened 7 months ago

jaksatomovic commented 7 months ago

I want to create book but instead of saving it to file and then storing it to db it would be nice to do this directly

aerkalov commented 7 months ago

You can do something like this. Instead of providing file name you can provide io.BytesIO object and then you can save content of that buffer from the memory to the file, database or anywhere you want. In this example I save it to the file but you can pass content of bk.getbuffer() anywhere you want.

    import io

    bk = io.BytesIO()

    epub.write_epub(bk, book, {})

    with open("book.epub", "wb") as f:
        f.write(bk.getbuffer())