Output format and password

On this page

The WordProcessingSaveOptions class is responsible for tuning and adjusting the saving process, when an EditableDocument instance, that contains already edited document content, should be converted to the output document of some WordProcessing format. In other words, the WordProcessingSaveOptions class specifies how exactly the edited HTML should be saved to the output document.

Unlike load and edit options, the WordProcessingSaveOptions class has one mandatory constructor parameter — output_format. The output_format parameter has a WordProcessingFormats type. In turn, this type contains all supported formats from the family of WordProcessing formats, which are supported for saving documents. Each field of WordProcessingFormats represents one separate WordProcessing format — DOC, DOCX, RTF, ODT etc. So, with the output_format parameter the user should select a specific format of the output WordProcessing document, which should be generated by the editor.save() method.

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

save_options = WordProcessingSaveOptions(WordProcessingFormats.RTF)

This constructor parameter is mandatory, because it is unacceptable to create a WordProcessingSaveOptions instance without an output format; otherwise, how would GroupDocs.Editor “know” into which specific format the document should be saved? However, the output format can be changed later, after creating an instance of the WordProcessingSaveOptions class, with the output_format property. This property also has a getter, that allows to obtain the output format, specified previously in the constructor.

save_options.output_format = WordProcessingFormats.DOCX

Almost all WordProcessing formats, especially those with a binary nature, support file encoding with a password. If such a document is encoded, it is required to specify a password for opening it. The WordProcessingSaveOptions class has the following property, which allows to set a password:

save_options.password = "p@ss"

By default the value of this property is None, which means that a password will not be applied. If the user specifies a string in this property, the output document will be encoded and protected with this string as a password. If the user has specified some password in this property at some step, but then wants to dismiss the password and not encode the document, he can set the value to None or an empty string — both these values will be interpreted as “do not set a password” when calling the editor.save() method.

Complete example

The example below loads the sample document, edits it, and saves the result to the RTF format encrypted with a password.

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

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

    with Editor("./sample-document.docx") as editor:
        original = editor.edit()
        modified = EditableDocument.from_markup(original.get_embedded_html())

        # Select the output format and protect the document with a password
        save_options = WordProcessingSaveOptions(WordProcessingFormats.RTF)
        save_options.password = "p@ss"

        editor.save(modified, "./protected-output.rtf", save_options)
        print("Saved a password-protected RTF document")

        original.dispose()
        modified.dispose()

if __name__ == "__main__":
    set_output_format_and_password()

sample-document.docx is the sample file used in this example. Click here to download it.

Binary file (RTF, 1.6 MB)

Download full output

On this page