Create and edit new WordProcessing document

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:

from groupdocs.editor import EditableDocument

html = "<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:

from groupdocs.editor.formats import WordProcessingFormats
from groupdocs.editor.options import WordProcessingSaveOptions

save_options = WordProcessingSaveOptions(WordProcessingFormats.DOCX)
save_options.enable_pagination = True

Complete code example

The example below builds an editable document from an HTML markup string, modifies its content, and saves the result to a DOCX file.

import os
from groupdocs.editor import Editor, EditableDocument, License
from groupdocs.editor.formats import WordProcessingFormats
from groupdocs.editor.options import WordProcessingSaveOptions

def create_document():
    # Optionally set a license
    license_path = os.path.abspath("./GroupDocs.Editor.lic")
    if os.path.exists(license_path):
        License().set_license(license_path)

    # Describe the content of the new document as HTML markup
    html = "<html><body><h1>New document</h1><p>Created with GroupDocs.Editor.</p></body></html>"

    # Modify the HTML content if needed
    updated_html = html.replace("New document", "My new document")

    # Build an editable document from the modified markup
    editable = EditableDocument.from_markup(updated_html)

    # Prepare the save options for the WordProcessing family
    save_options = WordProcessingSaveOptions(WordProcessingFormats.DOCX)
    save_options.enable_pagination = True

    # Load a sample document to obtain an Editor instance, then save the new document
    with Editor("./sample-document.docx") as editor:
        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.

Binary file (DOCX, 8 KB)

Download full output

Result

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.

See also