Create and edit new WordProcessing document
Leave feedback
On this page
This guide shows how to build a new document from HTML markup and save it in a specific format using the Editor and EditableDocument classes from GroupDocs.Editor for Python via .NET.
It is possible to create a new document in all major document formats, including text (DOCX), workbooks (XLSX), presentations (PPTX), e-books (EPUB) and emails (EML). See the full list of supported document formats, taking note of the “Create” column, which indicates whether it is possible to create a new document of a particular format.
Steps to Create and Edit a Document
1. Build an editable document from HTML markup
The content of a new document is described as HTML markup. Wrap that markup into an EditableDocument using the from_markup class method:
fromgroupdocs.editorimportEditableDocumenthtml="<html><body><h1>New document</h1><p>Created with GroupDocs.Editor.</p></body></html>"editable=EditableDocument.from_markup(html)
2. Modify the HTML content
If you obtained the markup from an existing source, you can modify it before building the editable document:
updated_html=html.replace("New document","My new document")updated_doc=EditableDocument.from_markup(updated_html)
3. Save the final document
Choose the appropriate save options for the desired output format and save the editable document. Saving requires an Editor instance, whose save() method writes the editable document to a file or stream:
The example below builds an editable document from an HTML markup string, modifies its content, and saves the result to a DOCX file.
importosfromgroupdocs.editorimportEditor,EditableDocument,Licensefromgroupdocs.editor.formatsimportWordProcessingFormatsfromgroupdocs.editor.optionsimportWordProcessingSaveOptionsdefcreate_document():# Optionally set a licenselicense_path=os.path.abspath("./GroupDocs.Editor.lic")ifos.path.exists(license_path):License().set_license(license_path)# Describe the content of the new document as HTML markuphtml="<html><body><h1>New document</h1><p>Created with GroupDocs.Editor.</p></body></html>"# Modify the HTML content if neededupdated_html=html.replace("New document","My new document")# Build an editable document from the modified markupeditable=EditableDocument.from_markup(updated_html)# Prepare the save options for the WordProcessing familysave_options=WordProcessingSaveOptions(WordProcessingFormats.DOCX)save_options.enable_pagination=True# Load a sample document to obtain an Editor instance, then save the new documentwithEditor("./sample-document.docx")aseditor:editor.save(editable,"./new-document.docx",save_options)editable.dispose()if__name__=="__main__":create_document()
sample-document.docx is the sample file used in this example. Click here to download it.
The code builds a DOCX document from HTML markup, modifies its body, and saves the final version to disk. You can optionally save it to an in-memory stream or return it via an API.