Security and privacy have always been important aspects of safeguarding data. Since Excel files are widely used to store data, safety is ensured to avoid tampering and unauthorized access. In accordance with that, this article covers how to encrypt and decrypt Excel files in Python.
- Python API to Encrypt and Decrypt Excel Files
- Encrypt Excel Files using Python
- Decrypt Excel Files using Python
Python API to Encrypt and Decrypt Excel Files
In order to encrypt and decrypt Excel files, we’ll use Aspose.Cells for Python via Java. Apart from the protection of Excel files, the API supports generating, modifying, and converting Excel files dynamically. You can install the API using the following pip command.
pip install aspose-cells
Encrypt Excel Files using Python
The following are the steps to encrypt Excel files in Python.
- Load the Excel file using the Workbook class.
- Set password using Workbook.getSettings().setPassword(string) method.
- Set type of the encryption using Workbook.setEncryptionOptions(EncryptionType, KeyLength) method.
- Save the encrypted Excel file using Workbook.save(string) method.
The following code sample shows how to encrypt Excel files in Python.
Decrypt Excel Files using Python
The following are the steps to decrypt Excel files in Python.
- Create an object of LoadOptions class and initialize it with XLSX format.
- Provide password using LoadOptions.setPassword(string) method.
- Create an object of the Workbook class and initialize it with an encrypted Excel file’s path and LoadOptions object.
- Decrypt Excel file by setting the password to None using Workbook.getSettings().setPassword(None).
- Save the decrypted Excel file using Workbook.save(string) method.
The following code sample shows how to decrypt an Excel file in Python.
Get a Free API License
You can use the API without evaluation limitations by requesting a free temporary license.
Conclusion
In this article, you have learned how to protect Excel files by applying encryption using Python. Furthermore, you have seen how to decrypt encrypted Excel files in Python. You can explore more about the Python spreadsheet API using the documentation. In case you would have any questions or queries, feel free to let us know via our forum.