ebooklib Package¶
ebooklib Package¶
epub Module¶
Backwards compatible public API for EbookLib.
Historically all of EbookLib lived in this module. The implementation has
been split into smaller modules (ebooklib.book, ebooklib.items,
ebooklib.reader, ebooklib.writer, ebooklib.toc,
ebooklib.consts and ebooklib.exceptions) but everything is
still importable from ebooklib.epub and will remain so until a future
major release.
- class ebooklib.epub.EpubBook[source]¶
Bases:
object- add_author(author: str, file_as: str | None = None, role: str | None = None, uid: str | None = None) None[source]¶
Add author for this document
- add_item(item: EpubItem) EpubItem[source]¶
Add additional item to the book. If not defined, media type and chapter id will be defined for the item.
A missing ID is generated as
chapter_N(EpubHtml),image_N(EpubImage) orstatic_N(any other item). Generated IDs skip IDs already used by items in the book. Explicit IDs are used as given and are not checked for uniqueness.- Args:
item: Item instance
- Returns:
The same item that was passed in, now attached to the book.
- add_metadata(namespace: str | None, name: str, value: str, others: dict | None = None) None[source]¶
Add metadata
- add_prefix(name: str, uri: str) None[source]¶
Appends custom prefix to be added to the content.opf document
>>> epub_book.add_prefix('bkterms', 'http://booktype.org/')
- Args:
name: namespave name
uri: URI for the namespace
- get_item_with_href(href: str) EpubItem | None[source]¶
Returns item for defined HREF.
>>> book.get_item_with_href('EPUB/document.xhtml')
- Args:
href: HREF for the item we are searching for
- Returns:
Returns item object. Returns None if nothing was found.
- get_item_with_id(uid: str) EpubItem | None[source]¶
Returns item for defined UID.
>>> book.get_item_with_id('image_001')
- Args:
uid: UID for the item
- Returns:
Returns item object. Returns None if nothing was found.
- get_items() Iterator[EpubItem][source]¶
Returns all items attached to this book.
- Returns:
Returns all items as tuple.
- get_items_of_media_type(media_type: str) Iterator[EpubItem][source]¶
Returns all items of specified media type.
- Args:
media_type: Media type for items we are searching for
- Returns:
Returns found items as tuple.
- get_items_of_type(item_type: int) Iterator[EpubItem][source]¶
Returns all items of specified type.
>>> book.get_items_of_type(epub.ITEM_IMAGE)
- Args:
item_type: Type for items we are searching for
- Returns:
Returns found items as tuple.
- get_template(name: str) bytes | None[source]¶
Returns value for the template.
- Args:
name: template name
- Returns:
Value of the template.
- set_cover(file_name: str, content: bytes, create_page: bool = True) None[source]¶
Set cover and create cover document if needed.
- Args:
file_name: file name of the cover page
content: Content for the cover image
create_page: Should cover page be defined. Defined as bool value (optional). Default value is True.
- set_direction(direction: str | None) None[source]¶
- Args:
direction: Options are “ltr”, “rtl” and “default”
- set_identifier(uid: str) None[source]¶
Sets unique id for this epub
- Args:
uid: Value of unique identifier for this book
- set_language(lang: str) None[source]¶
Set language for this epub. You can set multiple languages. Specific items in the book can have different language settings.
- Args:
lang: Language code
- set_template(name: str, value: bytes) None[source]¶
Defines templates which are used to generate certain types of pages. When defining new value for the template we have to use content of type ‘bytes’.
- At the moment we use these templates:
ncx
nav
chapter
cover
- Args:
name: Name for the template
value: Content for the template
- class ebooklib.epub.EpubCover(uid: str | None = 'cover-img', file_name: str = '')[source]¶
Bases:
EpubItemRepresents Cover image in the EPUB file.
- get_type() int[source]¶
Guess type according to the file extension. Might not be the best way how to do it, but it works for now.
- Items can be of type:
ITEM_UNKNOWN = 0
ITEM_IMAGE = 1
ITEM_STYLE = 2
ITEM_SCRIPT = 3
ITEM_NAVIGATION = 4
ITEM_VECTOR = 5
ITEM_FONT = 6
ITEM_VIDEO = 7
ITEM_AUDIO = 8
ITEM_DOCUMENT = 9
ITEM_COVER = 10
We map type according to the extensions which are defined in ebooklib.EXTENSIONS.
- Returns:
Returns type of the item as number.
- class ebooklib.epub.EpubCoverHtml(uid: str = 'cover', file_name: str = 'cover.xhtml', image_name: str = '', title: str = 'Cover')[source]¶
Bases:
EpubHtmlRepresents Cover page in the EPUB file.
- class ebooklib.epub.EpubHtml(uid: str | None = None, file_name: str = '', media_type: str = '', content: str | bytes | None = None, title: str = '', lang: str | None = None, direction: str | None = None, media_overlay: str | None = None, media_duration: str | None = None)[source]¶
Bases:
EpubItemRepresents HTML document in the EPUB file.
- add_item(item: EpubItem) None[source]¶
Add other item to this document. It will create additional links according to the item type.
- Args:
item: item we want to add defined as instance of EpubItem
- add_link(**kwgs: Any) None[source]¶
Add additional link to the document. Links will be embeded only inside of this document.
>>> add_link(href='styles.css', rel='stylesheet', type='text/css')
- add_meta(**kwgs: Any) None[source]¶
Add additional <meta> to the document.
>>> add_meta(name='viewport', content='width=device-width, initial-scale=1')
- get_body_content() bytes[source]¶
Returns content of BODY element for this HTML document. Content will be of type ‘bytes’.
Text placed directly in the BODY (before the first child element) is kept, so a body that contains only text is returned too. A body whose only direct text is whitespace (for example the newline and indentation before the first element) and that has no child elements is considered empty.
- Returns:
Returns content of this document, or
b""if the BODY is empty.
- get_content(default: str | bytes | None = None) bytes[source]¶
Returns content for this document as HTML string. Content will be of type ‘bytes’.
- Args:
default: Default value for the content if it is not defined.
- Returns:
Returns content of this document.
- get_language() str | None[source]¶
Get language code for this book item. Language of the book item can be different from the language settings defined globaly for book.
- Returns:
As string returns language code.
- get_links() Iterator[dict[str, Any]][source]¶
Returns list of additional links defined for this document.
- Returns:
As tuple return list of links.
- get_links_of_type(link_type: str) Iterator[dict[str, Any]][source]¶
Returns list of additional links of specific type.
- Returns:
As tuple returns list of links.
- get_metas() Iterator[dict[str, Any]][source]¶
Returns list of additional metas defined for this document.
- Returns:
As tuple return list of metas.
- class ebooklib.epub.EpubImage(uid: str | None = None, file_name: str = '', media_type: str = '', content: str | bytes | None = None, manifest: bool = True)[source]¶
Bases:
EpubItemRepresents Image in the EPUB file.
- get_type() int[source]¶
Guess type according to the file extension. Might not be the best way how to do it, but it works for now.
- Items can be of type:
ITEM_UNKNOWN = 0
ITEM_IMAGE = 1
ITEM_STYLE = 2
ITEM_SCRIPT = 3
ITEM_NAVIGATION = 4
ITEM_VECTOR = 5
ITEM_FONT = 6
ITEM_VIDEO = 7
ITEM_AUDIO = 8
ITEM_DOCUMENT = 9
ITEM_COVER = 10
We map type according to the extensions which are defined in ebooklib.EXTENSIONS.
- Returns:
Returns type of the item as number.
- class ebooklib.epub.EpubItem(uid: str | None = None, file_name: str = '', media_type: str = '', content: str | bytes | None = None, manifest: bool = True)[source]¶
Bases:
objectBase class for the items in a book.
- get_content(default: str | bytes | None = None) str | bytes[source]¶
Returns content of the item. Content is usually of type ‘bytes’ but API users assign ‘str’ as well.
- Args:
default: Default value for the content if it is not already defined.
- Returns:
Returns content of the item.
- get_id() str | None[source]¶
Returns unique identifier for this item.
- Returns:
Returns uid number as string.
- get_name() str[source]¶
Returns name for this item. By default it is always file name but it does not have to be.
- Returns:
Returns file name for this item.
- get_type() int[source]¶
Guess type according to the file extension. Might not be the best way how to do it, but it works for now.
- Items can be of type:
ITEM_UNKNOWN = 0
ITEM_IMAGE = 1
ITEM_STYLE = 2
ITEM_SCRIPT = 3
ITEM_NAVIGATION = 4
ITEM_VECTOR = 5
ITEM_FONT = 6
ITEM_VIDEO = 7
ITEM_AUDIO = 8
ITEM_DOCUMENT = 9
ITEM_COVER = 10
We map type according to the extensions which are defined in ebooklib.EXTENSIONS.
- Returns:
Returns type of the item as number.
Bases:
EpubHtmlRepresents Navigation Document in the EPUB file.
Returns if this document is chapter or not.
- Returns:
Returns book value.
- class ebooklib.epub.EpubNcx(uid: str | None = 'ncx', file_name: str = 'toc.ncx')[source]¶
Bases:
EpubItemRepresents Navigation Control File (NCX) in the EPUB.
- class ebooklib.epub.EpubReader(epub_file_name, options: dict[str, Any] | None = None)[source]¶
Bases:
object- DEFAULT_OPTIONS: dict[str, Any] = {'ignore_ncx': True}¶
- class ebooklib.epub.EpubSMIL(uid: str | None = None, file_name: str = '', content: str | bytes | None = None)[source]¶
Bases:
EpubItem- get_type() int[source]¶
Guess type according to the file extension. Might not be the best way how to do it, but it works for now.
- Items can be of type:
ITEM_UNKNOWN = 0
ITEM_IMAGE = 1
ITEM_STYLE = 2
ITEM_SCRIPT = 3
ITEM_NAVIGATION = 4
ITEM_VECTOR = 5
ITEM_FONT = 6
ITEM_VIDEO = 7
ITEM_AUDIO = 8
ITEM_DOCUMENT = 9
ITEM_COVER = 10
We map type according to the extensions which are defined in ebooklib.EXTENSIONS.
- Returns:
Returns type of the item as number.
- class ebooklib.epub.EpubWriter(name: str | PathLike[str] | _WritableFile, book: EpubBook, options: dict[str, Any] | None = None)[source]¶
Bases:
object- DEFAULT_OPTIONS: dict[str, Any] = {'compresslevel': 6, 'epub2_guide': True, 'epub3_landmark': True, 'epub3_pages': True, 'landmark_title': 'Guide', 'package_direction': False, 'pages_title': 'Pages', 'play_order': {'enabled': False, 'start_from': 1}, 'raise_exceptions': True, 'spine_direction': True}¶
- ebooklib.epub.read_epub(name, options: dict[str, Any] | None = None) EpubBook[source]¶
Creates new instance of EpubBook with the content defined in the input file.
>>> book = ebooklib.read_epub('book.epub')
- Args:
name: full path to the input file
options: extra options as dictionary (optional)
- Returns:
Instance of EpubBook.
- ebooklib.epub.write_epub(name: str | PathLike[str] | _WritableFile, book: EpubBook, options: dict[str, Any] | None = None) bool[source]¶
Creates epub file with the content defined in EpubBook.
>>> ebooklib.write_epub('book.epub', book)
- Args:
name: file name for the output file
book: instance of EpubBook
options: extra opions as dictionary (optional)
utils Module¶
- class ebooklib.utils.Directory(directory_path: str | PathLike)[source]¶
Bases:
objectReads files from an unpacked EPUB directory, using the same interface as ZipFile.
- read(subname: str) bytes[source]¶
Read the file
subnamerelative to the EPUB directory and return its bytes.subnametypically comes from OPF manifesthrefattributes, which are attacker-controlled and URL-decoded, so the resolved path must stay inside the EPUB directory.- Parameters:
subname – Path of the file, relative to the EPUB directory.
- Returns:
Raw content of the file.
- Raises:
OSError – If
subnameescapes the EPUB directory (path traversal, e.g."../../secret"), or if the file cannot be read.