The password property, if set, enables document protection from opening by encrypting it with the specified password. However, almost all WordProcessing formats support a document protection from writing, which is completely different from opening. Document protection, like document encoding, also implies a password as a form of key, but it also supports different levels of protection: some of them allow only read-only mode, others allow to edit form-fields etc.
GroupDocs.Editor allows to apply the document protection via the protection property in the WordProcessingSaveOptions class. By default this property has a None value, which means that GroupDocs.Editor will not apply the protection to the document.
The protection property has a WordProcessingProtection type. This type is a class, which, in turn, consists of two properties: a password and a protection type.
The password is responsible for setting a password for the document protection. By default it is None — protection is not applied. If user wants to apply the document protection, he needs to assign some string to this property; otherwise protection will not be applied regardless from the value of the protection type property.
The protection type property determines the level of protection. By default it has a NoProtection value, which means that no protection will be applied. Other values are:
AllowOnlyRevisions — User can only add revision marks to the document.
AllowOnlyComments — User can only modify comments in the document.
AllowOnlyFormFields — User can only enter data in the form fields in the document.
ReadOnly — No changes are allowed to the document.
Take a note that both the password and the protection type are bound to each other. If user sets a valid non-None, not-empty password, but the protection type property has a NoProtection value, the protection will not be applied. And vice versa, if the protection type property has an AllowOnlyFormFields value, for example, but the password is None or an empty string, then the protection will not be applied either.
The snippet below illustrates how the protection property is assigned on the WordProcessingSaveOptions instance before saving (the WordProcessingProtection and WordProcessingProtectionType types come from the groupdocs.editor.options module):
fromgroupdocs.editor.formatsimportWordProcessingFormatsfromgroupdocs.editor.optionsimport(WordProcessingSaveOptions,WordProcessingProtection,WordProcessingProtectionType,)save_options=WordProcessingSaveOptions(WordProcessingFormats.DOCX)# Make the resultant document read-only and protect this restriction with a passwordsave_options.protection=WordProcessingProtection(WordProcessingProtectionType.READ_ONLY,"write_password")
Complete example
Document protection (write protection) is separate from document encryption (protection from opening). The runnable example below shows the safe and most common scenario — loading the sample document, editing it, and saving the result encrypted with the password property so that the output document can be opened only with this password.
importosfromgroupdocs.editorimportEditor,EditableDocument,Licensefromgroupdocs.editor.formatsimportWordProcessingFormatsfromgroupdocs.editor.optionsimportWordProcessingSaveOptionsdefprotect_document():# Optionally set a licenselicense_path=os.path.abspath("./GroupDocs.Editor.lic")ifos.path.exists(license_path):License().set_license(license_path)withEditor("./sample-document.docx")aseditor:# Open the document for editingoriginal=editor.edit()# Edit the content (here done programmatically)modified_content=original.get_embedded_html().replace("Title of the document","Title of the protected document")modified=EditableDocument.from_markup(modified_content)# Encrypt the output document with a password (protection from opening)save_options=WordProcessingSaveOptions(WordProcessingFormats.DOCX)save_options.password="p@ss"editor.save(modified,"./protected-document.docx",save_options)print("Saved a password-protected document")original.dispose()modified.dispose()if__name__=="__main__":protect_document()
sample-document.docx is the sample file used in this example. Click here to download it.