All three formats are fully supported on both import (load) and export (save).
Load e-Book files for edit
GroupDocs.Editor for Python via .NET does not contain loading options either for the whole e-Book formats family or for the specific e-Book formats — users should specify e-Books through a file path or a byte stream without any loading options at all.
fromgroupdocs.editorimportEditor# Load from a file patheditor_from_path=Editor("book.epub")# Load from a binary streamwithopen("book.epub","rb")asstream:editor_from_stream=Editor(stream)
Edit e-Book files
There is a common edit options class for the whole e-Book formats family — the EbookEditOptions class. The content of this class resembles the content of the WordProcessingEditOptions class, because EbookEditOptions contains a subset of options from it — enable_pagination and enable_language_information — and, as in WordProcessingEditOptions, they are disabled (False) by default.
enable_pagination — allows enabling or disabling pagination in the resultant HTML document. By default it is disabled (False). This option controls how exactly the content of the e-Book will be converted to the EditableDocument representation while edited — in the float (False) or in the paged (True) mode.
enable_language_information — allows exporting (True) or not exporting (False) the language information to the resultant HTML markup. By default it is disabled (False). This is useful when an e-Book contains text in different languages, and you want to preserve this language-specific metainformation while editing the document in the WYSIWYG-editor.
Like for all supported document formats, the EbookEditOptions are optional, and the user may call the parameterless edit() method — in this case the default EbookEditOptions is implicitly applied.
Save e-Book files after edit
Saving e-Books is performed like for all other formats. When the e-Book content was edited by the client in the WYSIWYG-editor and sent back to the server-side, it should be passed to the EditableDocument, and then this instance should be passed to the editor.save() method.
For saving in one of the e-Book formats the EbookSaveOptions class should be used. This class is common for all supported e-Book formats within the e-Book family: MOBI, AZW3, and ePub. It has one constructor with a mandatory parameter — the desired output format, which should be specified as one of the EBookFormats values: MOBI, AZW3, or EPUB. Once the instance was created, this format can be obtained and changed using the output_format property.
split_heading_level of integer type controls how (if at all) to split the content of the e-Book into packages in the resultant file. It does not affect the representation of a file opened in any e-Book reader; rather, it is about the internal structure of the e-Book file. The default value is 2. Setting it to 0 disables splitting.
export_document_properties of boolean type controls whether to export built-in and custom document properties inside the resultant e-Book file. The default False value disables exporting document properties, so the resultant document will be a bit smaller in size.
The runnable example below loads an ePub file, edits it with default options, and saves the edited version back to the ePub format.
importosfromgroupdocs.editorimportEditor,EditableDocument,Licensefromgroupdocs.editor.formatsimportEBookFormatsfromgroupdocs.editor.optionsimportEbookEditOptions,EbookSaveOptionsdefedit_ebook():# Optionally set a licenselicense_path=os.path.abspath("./GroupDocs.Editor.lic")ifos.path.exists(license_path):License().set_license(license_path)# Load an input ePub file into the EditorwithEditor("./sample-ebook.epub")aseditor:# Create and adjust the e-Book edit optionsedit_options=EbookEditOptions()edit_options.enable_pagination=Trueedit_options.enable_language_information=True# Edit the e-Book and obtain an EditableDocumenteditable=editor.edit(edit_options)# Edit the content programmatically (in practice this is done in a WYSIWYG-editor)html=editable.get_embedded_html()edited=EditableDocument.from_markup(html.replace("</p>"," (edited)</p>",1))# Create ePub save options and tune themsave_options=EbookSaveOptions(EBookFormats.EPUB)save_options.export_document_properties=Truesave_options.split_heading_level=3# Save the edited document back to the ePub formateditor.save(edited,"./edited-ebook.epub",save_options)editable.dispose()edited.dispose()if__name__=="__main__":edit_ebook()
sample-ebook.epub is the sample file used in this example. Click here to download it.
The same approach is used to save into the AZW3 or MOBI formats — just create the EbookSaveOptions instance with EBookFormats.AZW3 or EBookFormats.MOBI in the constructor.
Extracting metainfo from e-Book files
Like for all supported formats, GroupDocs.Editor for Python via .NET provides the ability to detect document metainfo for all supported e-Book formats by using the get_document_info() method of the Editor class. When a valid e-Book was loaded into the Editor instance, get_document_info() returns a metadata view corresponding to the EbookDocumentInfo type, which defines the format, page_count, size, and is_encrypted properties.
The format property returns an EBookFormats value, which for e-Books can be MOBI, AZW3, or EPUB.
The page_count property returns an approximate number of pages in the case of MOBI or AZW3, or the number of chapters in the case of ePub. The returned number should be treated carefully and approximately.
The size property returns the number of bytes of the e-Book file.
The is_encrypted property always returns False, because e-Books cannot be encrypted with a password.