ebooklib Package — EbookLib 0.20 documentation

# ebooklib Package

## `ebooklib` Package

## `epub` Module

class ebooklib.epub.EpubBook

  Bases: `object`

  add_author(author, file_as=None, role=None, uid='creator')

    Add author for this document

  add_item(item)

    Add additional item to the book. If not defined, media type and chapter id will be defined for the item.

    Args:

      - item: Item instance

  add_metadata(namespace, name, value, others=None)

    Add metadata

  add_prefix(name, uri)

    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)

    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)

    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()

    Returns all items attached to this book.

    Returns:

      Returns all items as tuple.

  get_items_of_media_type(media_type)

    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)

    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_metadata(namespace, name)

    Retrieve metadata

  get_template(name)

    Returns value for the template.

    Args:

      - name: template name

    Returns:

      Value of the template.

  reset()

    Initialises all needed variables to default values

  set_cover(file_name, content, create_page=True)

    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)
    Args:

      - direction: Options are “ltr”, “rtl” and “default”

  set_identifier(uid)

    Sets unique id for this epub

    Args:

      - uid: Value of unique identifier for this book

  set_language(lang)

    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, value)

    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 ‘str’ (Python 2) or ‘bytes’ (Python 3).

    At the moment we use these templates:

      - ncx

      - nav

      - chapter

      - cover

    Args:

      - name: Name for the template

      - value: Content for the template

  set_title(title)

    Set title. You can set multiple titles.

    Args:

      - title: Title value

  set_unique_metadata(namespace, name, value, others=None)

    Add metadata if metadata with this identifier does not already exist, otherwise update existing metadata.

class ebooklib.epub.EpubCover(uid='cover-img', file_name='')

  Bases: `EpubItem`

  Represents Cover image in the EPUB file.

  get_type()

    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='cover', file_name='cover.xhtml', image_name='', title='Cover')

  Bases: `EpubHtml`

  Represents Cover page in the EPUB file.

  get_content()

    Returns content for cover page as HTML string. Content will be of type ‘str’ (Python 2) or ‘bytes’ (Python 3).

    Returns:

      Returns content of this document.

  is_chapter()

    Returns if this document is chapter or not.

    Returns:

      Returns book value.

exception ebooklib.epub.EpubException(code, msg)

  Bases: `Exception`

class ebooklib.epub.EpubHtml(uid=None, file_name='', media_type='', content=None, title='', lang=None, direction=None, media_overlay=None, media_duration=None)

  Bases: `EpubItem`

  Represents HTML document in the EPUB file.

  add_item(item)

    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)

    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)

    Add additional <meta> to the document.

        >>> add_meta(name='viewport', content='width=device-width, initial-scale=1')

  get_body_content()

    Returns content of BODY element for this HTML document. Content will be of type ‘str’ (Python 2) or ‘bytes’ (Python 3).

    Returns:

      Returns content of this document.

  get_content(default=None)

    Returns content for this document as HTML string. Content will be of type ‘str’ (Python 2) or ‘bytes’ (Python 3).

    Args:

      - default: Default value for the content if it is not defined.

    Returns:

      Returns content of this document.

  get_language()

    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()

    Returns list of additional links defined for this document.

    Returns:

      As tuple return list of links.

  get_links_of_type(link_type)

    Returns list of additional links of specific type.

    Returns:

      As tuple returns list of links.

  get_metas()

    Returns list of additional metas defined for this document.

    Returns:

      As tuple return list of metas.

  get_type()

    Always returns ebooklib.ITEM_DOCUMENT as type of this document.

    Returns:

      Always returns ebooklib.ITEM_DOCUMENT

  is_chapter()

    Returns if this document is chapter or not.

    Returns:

      Returns book value.

  set_language(lang)

    Sets language for this book item. By default it will use language of the book but it can be overwritten with this call.

class ebooklib.epub.EpubImage(*args, **kwargs)

  Bases: `EpubItem`

  Represents Image in the EPUB file.

  get_type()

    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=None, file_name='', media_type='', content=None, manifest=True)

  Bases: `object`

  Base class for the items in a book.

  get_content(default=None)

    Returns content of the item. Content should be of type ‘str’ (Python 2) or ‘bytes’ (Python 3)

    Args:

      - default: Default value for the content if it is not already defined.

    Returns:

      Returns content of the item.

  get_id()

    Returns unique identifier for this item.

    Returns:

      Returns uid number as string.

  get_name()

    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()

    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.

  set_content(content)

    Sets content value for this item.

    Args:

      - content: Content value

class ebooklib.epub.EpubNav(uid='nav', file_name='nav.xhtml', media_type='application/xhtml+xml', title='', direction=None)

  Bases: `EpubHtml`

  Represents Navigation Document in the EPUB file.

  is_chapter()

    Returns if this document is chapter or not.

    Returns:

      Returns book value.

class ebooklib.epub.EpubNcx(uid='ncx', file_name='toc.ncx')

  Bases: `EpubItem`

  Represents Navigation Control File (NCX) in the EPUB.

class ebooklib.epub.EpubReader(epub_file_name, options=None)

  Bases: `object`

  DEFAULT_OPTIONS = {'ignore_ncx': True}
  load()
  process()
  read_file(name)

class ebooklib.epub.EpubSMIL(uid=None, file_name='', content=None)

  Bases: `EpubItem`

  get_type()

    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, book, options=None)

  Bases: `object`

  DEFAULT_OPTIONS = {'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': False, 'spine_direction': True}
  process()
  write()

class ebooklib.epub.Link(href, title, uid=None)

  Bases: `object`

class ebooklib.epub.Section(title, href='')

  Bases: `object`

ebooklib.epub.read_epub(name, options=None)

  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, book, options=None)

  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)

  Bases: `object`

  close()
  read(subname)

ebooklib.utils.create_pagebreak(pageref, label=None, html=True)

ebooklib.utils.debug(obj)

ebooklib.utils.get_headers(elem)

ebooklib.utils.get_pages(item)

ebooklib.utils.get_pages_for_items(items)

ebooklib.utils.guess_type(extenstion)

ebooklib.utils.parse_html_string(s)

ebooklib.utils.parse_string(s)

## Subpackages

- plugins Package
  - `base` Module
    - `BasePlugin`
      - `BasePlugin.after_read()`
      - `BasePlugin.after_write()`
      - `BasePlugin.before_read()`
      - `BasePlugin.before_write()`
      - `BasePlugin.html_after_read()`
      - `BasePlugin.html_before_write()`
      - `BasePlugin.item_after_read()`
      - `BasePlugin.item_before_write()`
  - `booktype` Module
    - `BooktypeFootnotes`
      - `BooktypeFootnotes.NAME`
      - `BooktypeFootnotes.html_before_write()`
    - `BooktypeLinks`
      - `BooktypeLinks.NAME`
      - `BooktypeLinks.html_before_write()`
  - `sourcecode` Module
    - `SourceHighlighter`
      - `SourceHighlighter.html_before_write()`
  - `standard` Module
    - `SyntaxPlugin`
      - `SyntaxPlugin.NAME`
      - `SyntaxPlugin.html_before_write()`
    - `leave_only()`
  - `tidyhtml` Module
    - `TidyPlugin`
      - `TidyPlugin.NAME`
      - `TidyPlugin.OPTIONS`
      - `TidyPlugin.html_after_read()`
      - `TidyPlugin.html_before_write()`
    - `tidy_cleanup()`