Converting Ebooks

Using Calibre’s ebook-convert’_ tool, converts files between supportedebook formats. The format of the desired output can be specified either by specifying the name of the output file, an ``EbookFormat` enum value, or the desired extenion.

..ebook-convert: https://manual.calibre-ebook.com/generated/en/ebook-convert.html

capybre.convert.convert(input_file, output_file=None, as_format=<EbookFormat.UNKNOWN: 0>, as_ext=None, suppress_output=True) → str

Converts ebook at input_file to new format, returning the converted filepath

Output format can be specified either through one of output_file, as_format, and as_ext. If the full output_file is not specified, the outputted file will have the same name as the input_file with a different extension

Parameters:
  • input_file (str) – path to the input file
  • output_file (str, optional) – fully-specified path to the output file
  • as_format (EbookFormat, optional) – Enum representation of desired output format
  • as_ext (str, optional) – String representation of desired output format, e.g. mobi
  • suppress_output (bool, optional) – Suppresses stdout from ebook-convert call (typically dozens of lines). Defaults to True
Returns:

Path to the output file

class capybre.convert.converted_fileobj(input_file, as_format=None, as_ext=None, suppress_output=True)

Context-object wrapper around convert

Using the with X as Y: style, creates a temporary converted file object, opened in read-binary mode, and deletes it when finished. For use like

with converted_fileobj('original.epub', target_extension='mobi') as f:
    # f is file pointer to file object
    upload(f)
Parameters:
  • input_file (str) – path to the input file
  • output_file (str, optional) – fully-specified path to the output file
  • as_format (EbookFormat, optional) – Enum representation of desired output format
  • as_ext (str, optional) – String representation of desired output format, e.g. mobi
  • suppress_output (bool, optional) – Suppresses stdout from ebook-convert call (typically dozens of lines). Defaults to True
class capybre.ebook_format.EbookFormat

EbookFormat is an enum representation of the supported output ebook formats: EPUB, LIT, LRF, FB2, MOBI, PDB, PDF, PMLZ, RB, TCR, TXT

to_ext() → str

Gets the extension for the given EbookFormat Enum value