Chiffrer et déchiffrer les fichiers Excel

Utilisation de Microsoft Excel

Pour définir les paramètres de chiffrement de fichier dans Microsoft Excel (ici Microsoft Excel 2003) :

  1. Dans le menu Outils, sélectionnez Options. Une boîte de dialogue apparaîtra.
  2. Sélectionnez l’onglet Sécurité.
  3. Saisissez un mot de passe et cliquez sur Avancé
  4. Choisissez le type de chiffrement et confirmez le mot de passe.

Chiffrer un fichier Excel avec Aspose.Cells

L’exemple suivant montre comment chiffrer et protéger par mot de passe un fichier Excel en utilisant l’API Aspose.Cells pour Python via .NET.

from aspose.cells import EncryptionType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Instantiate a Workbook object.
# Open an excel file.
workbook = Workbook(dataDir + "Book1.xls")
# Specify XOR encryption type.
workbook.set_encryption_options(EncryptionType.XOR, 40)
# Specify Strong Encryption type (RC4,Microsoft Strong Cryptographic Provider).
workbook.set_encryption_options(EncryptionType.STRONG_CRYPTOGRAPHIC_PROVIDER, 128)
# Password protect the file.
workbook.settings.password = "1234"
# Save the excel file.
workbook.save(dataDir + "encryptedBook1.out.xls")

Option de spécification du mot de passe pour modifier

L’exemple suivant montre comment définir l’option Mot de passe pour modifier Microsoft Excel pour un fichier existant en utilisant l’API Aspose.Cells pour Python via .NET.

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Instantiate a Workbook object.
# Open an excel file.
workbook = Workbook(dataDir + "Book1.xls")
# Set the password for modification.
workbook.settings.write_protection.password = "1234"
# Save the excel file.
workbook.save(dataDir + "SpecifyPasswordToModifyOption.out.xls")

Déchiffrer le fichier Excel avec Aspose.Cells

Il est très facile d’ouvrir un fichier Excel protégé par mot de passe et de le déchiffrer en utilisant l’API Aspose.Cells pour Python via .NET avec les codes suivants :

from aspose.cells import LoadOptions, Workbook
# Open encrypted file with password.
loadOptions = LoadOptions()
loadOptions.password = "password"
workbook = Workbook("Book1.xlsx", loadOptions)
# Remove password.
workbook.settings.password = None
# Save the file.
workbook.save("Book1.xlsx")

Sujets avancés