analitics

Pages

Showing posts with label ebooklib. Show all posts
Showing posts with label ebooklib. Show all posts

Monday, February 27, 2023

Python 3.11.0 : EbookLib python library - part 002.

The last version 3.3 of this file type was published by the EPUB 3 Working Group as a Candidate Recommendation Snapshot on 21 February 2023 ...
I haven't written about the ePub format and python since 2022 because I was busy with all kinds of tasks.
Then I presented a short introduction with some examples on how to create an ePUB file, see that tutorial article.
I like this file type, especially the last format that allows many features, including inserting SVG vector files.
Installing the package is easy on a windows 10 with the pip utility.
pip install ebooklib --user
In the folder with the svg image named cover.svg I created a python file with this content.
from ebooklib import epub

# Create a new EPUB book
book = epub.EpubBook()

# Set the book metadata
book.set_title('My EPUB Book')
book.set_language('en')

# Add a cover image
cover_svg = epub.EpubItem(uid="cover_svg", file_name="cover.svg", media_type="image/svg+xml", content=open("cover.svg").read())
book.add_item(cover_svg)

# Create a new chapter for the cover page
cover_page = epub.EpubHtml(title='Cover Page', file_name='cover.xhtml', lang='en')
cover_page.content = '<img alt="Cover Image" src="cover.svg" />'

# Add the cover page to the book
book.add_item(cover_page)

# Add a chapter to the book
chapter1 = epub.EpubHtml(title='Chapter 1', file_name='chap_1.xhtml', lang='en')
chapter1.content = '<h1>Chapter 1</h1><p>This is the first chapter of my book.</p>'

# Add the chapter to the book
book.add_item(chapter1)

# Create the spine
book.spine = [cover_page, chapter1]

# Generate the EPUB file
epub.write_epub('my_book.epub', book, {})
after running the script this was the result a epub file named my_book.epub.
The result is this simple epub with an SVG file from Wikipedia, see the screenshot take with the Sumatra PDF free software.

Monday, August 8, 2022

Python 3.10.4 : EbookLib python library - part 001.

EbookLib is a Python library for managing EPUB2/EPUB3 files. It’s capable of reading and writing EPUB files programmatically.
You can read more about this python library on this website.
First, I install with the pip tool:
C:\Python310>python -m pip install --upgrade pip
Requirement already satisfied: pip in c:\python310\lib\site-packages (22.1)
Collecting pip
  WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, stat
us=None)) after connection broken by 'NewConnectionError(': Failed to establish a n
ew connection: [Errno 11001] getaddrinfo failed')': /packages/1f/2c/d9626f045e7b
49a6225c6b09257861f24da78f4e5f23af2ddbdf852c99b8/pip-22.2.2-py3-none-any.whl
  Downloading pip-22.2.2-py3-none-any.whl (2.0 MB)
     ---------------------------------------- 2.0/2.0 MB 1.5 MB/s eta 0:00:00
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 22.1
    Uninstalling pip-22.1:
      Successfully uninstalled pip-22.1
Successfully installed pip-22.2.2

C:\Python310>python -m pip install lxml-4.9.0-cp310-cp310-win_amd64.whl
Processing c:\python310\lxml-4.9.0-cp310-cp310-win_amd64.whl
Installing collected packages: lxml
Successfully installed lxml-4.9.0

C:\Python310>python -m pip install EbookLib --user
Collecting EbookLib
  Using cached EbookLib-0.17.1.tar.gz (111 kB)
  Preparing metadata (setup.py) ... done
Requirement already satisfied: lxml in c:\python310\lib\site-packages (from Eboo
kLib) (4.9.0)
Collecting six
  Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Using legacy 'setup.py install' for EbookLib, since package 'wheel' is not insta
lled.
Installing collected packages: six, EbookLib
  Running setup.py install for EbookLib ... done
Successfully installed EbookLib-0.17.1 six-1.16.0
I used a the simple python script from the last tutorial to test it:
The last tutorial used the default script from the official webpage.
Compared to the default script, I made changes, the selection in the Romanian language and diacritics...
from ebooklib import epub

book = epub.EpubBook()

# set metadata
book.set_identifier('__1976')
book.set_title('')
book.set_language('en')

book.add_author('Autho: Cătălin George Feștilă')

# create chapter
cap001 = epub.EpubHtml(title='Intro', file_name='capitolul_01.xhtml', lang='ro')
cap001.content=u'<h1>Introducere</h1><p>Această carte este...</p>'

# add chapter
book.add_item(cap001)

# define Table Of Contents
book.toc = (epub.Link('capitolul_01.xhtml', 'Introducere', 'introducere'),
             (epub.Section('O carte simplă'),
             (cap001, ))
            )

# add default NCX and Nav file
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())

# define CSS style
style = 'BODY {color: white;}'
nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style)

# add CSS file
book.add_item(nav_css)

# basic spine
book.spine = ['nav', cap001]

# write to the file
epub.write_epub('ro_lan_test.epub', book, {})
This is the result of the epub file:

Wednesday, January 3, 2018

The ebooklib python module .

Happy new year 2018!
The official webpage of this python module comes with this intro:
EbookLib is a Python library for managing EPUB2/EPUB3 and Kindle files. It's capable of reading and writing EPUB files programmatically (Kindle support is under development).
First the installation of this python module named ebooklib.
C:\>cd Python27

C:\Python27>cd Script
The system cannot find the path specified.

C:\Python27>cd Scripts

C:\Python27\Scripts>pip install ebooklib
Collecting ebooklib
  Downloading EbookLib-0.16.tar.gz
Requirement already satisfied: lxml in c:\python27\lib\site-packages (from ebooklib)
Requirement already satisfied: six in c:\python27\lib\site-packages (from ebooklib)
Installing collected packages: ebooklib
  Running setup.py install for ebooklib ... done
Successfully installed ebooklib-0.16
If you don't see the Scripts folder into your Python27 folder you need to install pip tool.
Just download the get-pip.py script into your Python27 folder and run it with python.
Let's test some default example:
C:\Python27>python.exe get-pip.py
The next step is to test a simple example:
from ebooklib import epub

book = epub.EpubBook()

# set metadata
book.set_identifier('id123456')
book.set_title('Sample book')
book.set_language('en')

book.add_author('Author Python')
book.add_author('catafest', file_as='', role='writer', uid='author')

# create chapter
c1 = epub.EpubHtml(title='Intro', file_name='chap_01.xhtml', lang='hr')
c1.content=u'Intro heading.Python is a interpreted high-level programming language ...'

# add chapter
book.add_item(c1)

# define Table Of Contents
book.toc = (epub.Link('chap_01.xhtml', 'Introduction', 'intro'),
(epub.Section('Simple book'),
(c1, ))
)

# add default NCX and Nav file
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())

# define CSS style
style = 'BODY {color: white;}'
nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style)

# add CSS file
book.add_item(nav_css)

# basic spine
book.spine = ['nav', c1]

# write to the file
epub.write_epub('test.epub', book, {})
You can update and make more good your epub book with HTML5 tags.
I used this example with headings and paragraph to change the text, see the result: