
MS Excel provides a quick and easy way to store small as well as large-sized data in rows and columns. It also lets you format data, perform calculations, analyze data, and create graphs and charts. Because data needs protection, Excel allows you to password‑protect files. In this article you will learn how to password‑protect Excel files in Python and how to protect a specific worksheet and unprotect Excel files dynamically.
- Python Library to Protect Excel Files - Free Download
- Simple Steps to Protect Excel Files in Python
- Protect Excel Files in Python
- Unprotect Excel Files in Python
- Protect Worksheets in Python
- Unprotect Worksheets in Python
- Get a Free License
Python Library to Protect Excel Files - Free Download
Aspose.Cells for Python is a powerful, feature‑rich library designed to create, edit, and convert Excel files from Python applications. We will use this library to protect and unprotect workbooks or worksheets. You can either download the package or install it from PyPI with the command:
pip install aspose-cells
Simple Steps to Protect Excel Files in Python
First, review the protection types Aspose.Cells for Python offers:
- ALL – User cannot modify anything
- CONTENTS – User cannot enter data
- OBJECTS – User cannot modify drawing objects
- SCENARIOS – User cannot modify saved scenarios
- STRUCTURE – User cannot modify workbook structure
- WINDOWS – User cannot modify window settings
- NONE – No protection
You can apply any of these types to your Excel files. Follow these simple steps:
- Load the Excel file from the desired location.
- Apply the required protection and set a password.
- Save the protected file.
Password-Protect Excel Files in Python
To password‑protect an Excel file, follow these steps:
- Load the file using the Workbook class.
- Call Workbook.protect(ProtectionType, password) to set protection.
- Save the file with Workbook.save(fileName).
The code sample below demonstrates how to protect a workbook:
Unprotect Excel Files in Python
Unprotecting a workbook is straightforward. Load the file, remove protection with the password, and save the result.
- Load the file using Workbook.
- Call Workbook.unprotect(password).
- Save the unprotected file with Workbook.save(fileName).
The following code shows how to unprotect a workbook:
Python: Apply Password-Protection to an Excel Worksheet
You can also protect a specific worksheet:
- Load the workbook with Workbook.
- Get the worksheet using Workbook.getWorksheets().get(index).
- Apply protection with Worksheet.protect(ProtectionType).
- Set a password via Worksheet.getProtection().setPassword(password).
- Save the workbook with Workbook.save(fileName).
Code example:
Unprotect Excel Worksheets in Python
To unprotect a worksheet, use the Worksheet.unprotect(password) method.
Python Library to Password-Protect Excel Files - Get a Free License
You can get a free temporary license to use Aspose.Cells for Python without evaluation limits.
Conclusion
You have learned how to password‑protect and unprotect Excel workbooks and worksheets in Python using Aspose.Cells. Explore more of the Python Excel automation API in the documentation. For questions, visit our forum.