The password dictionary can be changed at any time. A password is stored with a key, which is the full path of a document file or, for a document added from a stream or a structure, the document key.
To get the number of passwords in the dictionary, use the count property.
To get the password for a document from the dictionary, the get_password method is used, passing the full path or the key of the document as an argument.
The clear method is used to remove all passwords from the dictionary.
To check for the presence of a password in the dictionary for the specified document, the contains method is used.
To add a password to the dictionary, use the add method. The first parameter is the full path or the key of the document. If the dictionary already contains a password for the document, the method raises ValueError; remove the existing password first.
To remove a password from the dictionary, use the remove method.
To import passwords from a file, use the import_dictionary method. The imported passwords are added to the dictionary, so the dictionary must not already contain passwords for the same documents; otherwise, the import fails. Errors of exporting and importing are reported by the error_occurred event of the index instead of exceptions.
The following example demonstrates the use of methods of the password dictionary.
importosfromgroupdocs.searchimportIndex# Creating or opening an index in the specified folderindex=Index("./index")passwords=index.dictionaries.document_passwordspath=os.path.abspath("./documents/protected.pdf")ifpasswords.contains(path):# Getting a password for a documentpassword=passwords.get_password(path)print(path)print("\tPassword: "+password)# Deleting the password from the dictionarypasswords.remove(path)ifpasswords.count>0:# Removing all passwords from the dictionarypasswords.clear()# Adding a password for a documentpasswords.add(path,"123456")# Exporting passwords to a filepasswords.export_dictionary("./Passwords.dat")# Removing all passwords and importing them from the filepasswords.clear()passwords.import_dictionary("./Passwords.dat")# Indexing documents; the passwords from the dictionary are used to open protected documentsindex.add("./documents")# Searching for a word of the protected documentresult=index.search("confidential")print("Documents found:",result.document_count)