Edit document

This article describes how to open a previously loaded document for editing, which options should be applied, and how to send the document content to the WYSIWYG HTML-editor or any other editing application.

When a document is loaded into an instance of the Editor class, it is possible to open it for editing. In terms of GroupDocs.Editor, opening a document for editing implies creating an instance of the EditableDocument class by calling the Editor.edit() instance method. The edit() method can be called with a single parameter — an edit options instance — or without any parameter at all.

Each format family has its own edit options class. They are listed in the table below.

Format familyExample formatsEdit options class
WordProcessingDOC, DOCX, DOCM, DOT, ODTWordProcessingEditOptions
SpreadsheetXLS, XLSX, XLSM, XLSBSpreadsheetEditOptions
Delimiter-Separated Values (DSV)CSV, TSVDelimitedTextEditOptions
PresentationPPT, PPTX, PPS, POTPresentationEditOptions
Plain Text documentsTXTTextEditOptions
Fixed-layout formatPDFPdfEditOptions
Fixed-layout formatXPS (including OpenXPS)XpsEditOptions
XMLAny XML-based format like CSPROJ, SVG, and so onXmlEditOptions
eBookMOBI, AZW3, ePubEbookEditOptions
EmailEML, EMLX, TNEF, MSG, HTML, MHTML, ICS, VCF, PST, MBOX, OFTEmailEditOptions

The parameterless edit() overload chooses the most appropriate default edit options based on the input document format.

The EditableDocument instance holds a version of the input document, converted to an internal intermediate format according to the edit options. When it is ready, it can emit HTML, CSS, and other appropriate content that can be passed directly to the WYSIWYG editor. This is demonstrated below.

from groupdocs.editor import Editor
from groupdocs.editor.options import WordProcessingLoadOptions, WordProcessingEditOptions

editor = Editor("document.docx", WordProcessingLoadOptions())
opened_document = editor.edit()  # with default edit options

edit_options = WordProcessingEditOptions()
edit_options.enable_language_information = True
edit_options.enable_pagination = True

another_opened_document = editor.edit(edit_options)

Several EditableDocument instances can be generated from a single Editor instance with different edit options. For example, for a WordProcessing document, the first time a user can call the edit() method with the paged mode disabled, and the second time with it enabled. In other words, several different EditableDocument representations of a single original document can be generated. Another example — there can be multiple EditableDocument instances for a single input Spreadsheet document, where each instance represents a different tab of the Spreadsheet document. Such an example is shown below.

from groupdocs.editor import Editor
from groupdocs.editor.options import SpreadsheetLoadOptions, SpreadsheetEditOptions

editor = Editor("spreadsheet.xlsx", SpreadsheetLoadOptions())

edit_options1 = SpreadsheetEditOptions()
edit_options1.worksheet_index = 0  # index is 0-based, so this is the 1st tab
edit_options1.exclude_hidden_worksheets = True

edit_options2 = SpreadsheetEditOptions()
edit_options2.worksheet_index = 1  # index is 0-based, so this is the 2nd tab
edit_options2.exclude_hidden_worksheets = False

first_tab = editor.edit(edit_options1)
second_tab = editor.edit(edit_options2)

When the EditableDocument instance is ready, it can emit HTML markup, CSS markup, and other resources in different forms for passing them to the client-side WYSIWYG HTML-editor or any other application that can edit HTML documents. This is briefly shown in the example below.

document = editor.edit()
body_content = document.get_body_content()
only_images = document.images
all_resources_together = document.all_resources

For more information about obtaining HTML markup and resources from EditableDocument, please visit the Get HTML markup in different forms and Save HTML to folder articles.