Excel files often contain sensitive and confidential information, such as financial data, personal information, trade secrets, or proprietary formulas. When sharing these files over the internet or between organizations, there might be a risk of data leaks, theft, or unauthorized modifications. To address this concern, Excel provides a comprehensive set of protection features, such as password-protecting workbooks, restricting editing on worksheets, and locking cells, which enable users to establish multiple layers of security to control data access and maintain the integrity of their Excel files. In this article, you will learn how to protect and unprotect Excel workbooks and worksheets in Python using Spire.XLS for Python.
- Password Protect an Entire Workbook
- Protect a Worksheet with a Specific Protection Type
- Allow Users to Edit Ranges in a Protected Worksheet
- Unprotect a Password Protected Worksheet
- Remove or Reset the Password of an Encrypted Workbook
Install Spire.XLS for Python
This scenario requires Spire.XLS for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip commands.
pip install Spire.XLS
If you are unsure how to install, please refer to this tutorial: How to Install Spire.XLS for Python on Windows
Password Protect an Entire Workbook in Python
By encrypting an Excel document with a password, you ensure that the data within the document remains secure and inaccessible to unauthorized individuals. The following are the steps to password-protect a workbook using Spire.XLS for Python.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Protect the workbook with a password using Workbook.Protect() method.
- Save the workbook to another Excel file using Workbook.SaveToFile() method.
- Python
from spire.xls import * from spire.xls.common import * # Create a Workbook object workbook = Workbook() # Load an Excel workbook workbook.LoadFromFile("Sample.xlsx") # Protect the workbook with a password workbook.Protect("psd-123") # Save the workbook to another Excel file workbook.SaveToFile("Encrypted.xlsx", ExcelVersion.Version2016) workbook.Dispose()
Protect a Worksheet with a Specific Protection Type in Python
If you want to authorize others to view your Excel document while limiting the types of changes they can make to a worksheet, you can protect the worksheet with a specific protection type. The table below lists a variety of pre-defined protection types under the SheetProtectionType enumeration.
Protection Type | Allow users to |
Content | Modify or insert content. |
DeletingColumns | Delete columns. |
DeletingRows | Delete rows. |
Filtering | Set filters. |
FormattingCells | Format cells. |
FormattingColumns | Format columns. |
FormattingRows | Format rows. |
InsertingColumns | Insert columns. |
InsertingRows | Insert rows. |
InsertingHyperlinks | Insert hyperlinks . |
LockedCells | Select locked cells. |
UnlockedCells | Select unlocked cells. |
Objects | Modify drawing objects. |
Scenarios | Modify saved scenarios. |
Sorting | Sort data. |
UsingPivotTables | Use pivot table and pivot chart. |
All | Do any operations listed above on the protected worksheet. |
none | Do nothing on the protected worksheet. |
The following steps show you how to protect a worksheet with a specific protection type using Spire.XLS for Python.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through Workbook.Worksheets[index] property.
- Protect the worksheet with a password and a specific protection type using Worksheet.Protect(password:str, options:SheetProtectionType) method.
- Save the workbook to another Excel file using Workbook.SaveToFile() method.
- Python
from spire.xls import * from spire.xls.common import * # Create a Workbook object workbook = Workbook() # Load an Excel workbook workbook.LoadFromFile("Sample.xlsx") # Get the first worksheet worksheet = workbook.Worksheets[0] # Protect the worksheet with a password and a specific protection type worksheet.Protect("psd-permission", SheetProtectionType.none) # Save the workbook to another Excel file workbook.SaveToFile("ProtectWorksheet.xlsx", ExcelVersion.Version2016) workbook.Dispose()
Allow Users to Edit Ranges in a Protected Worksheet in Python
In certain cases, you may want to allow users to edit certain ranges of a worksheet while preserving the integrity of other data. The following steps demonstrate how to accomplish this feature using Spire.XLS for Python.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through Workbook.Worksheets[index] property.
- Specify editable cell ranges using Worksheet.AddAllowEditRange() method.
- Protect the worksheet with a password and a specific protection type using Worksheet.Protect(password:str, options:SheetProtectionType) method.
- Save the workbook to another Excel file using Workbook.SaveToFile() method.
- Python
from spire.xls import * from spire.xls.common import * # Create a Workbook object workbook = Workbook() # Load an Excel workbook workbook.LoadFromFile("Sample.xlsx") # Get the first worksheet sheet = workbook.Worksheets[0] # Add ranges that allow editing sheet.AddAllowEditRange("Range One", sheet.Range["A5:A6"]) sheet.AddAllowEditRange("Range Two", sheet.Range["A8:B11"]) # Protect the worksheet with a password and a protection type sheet.Protect("psd-permission", SheetProtectionType.All) # Save the workbook to another Excel file workbook.SaveToFile("AllowEditRange.xlsx", ExcelVersion.Version2016) workbook.Dispose()
Unprotect a Password Protected Worksheet in Python
To remove the protection of a password-protected worksheet, you need to invoke the Worksheet.Unprotect() method and pass the original password to the method as a parameter. The detailed steps are as follows.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through Workbook.Worksheets[index] property.
- Remove the password protection using Worksheet.Unprotect(password:str) method.
- Save the workbook to another Excel file using Workbook.SaveToFile() method.
- Python
from spire.xls import * from spire.xls.common import * # Create a Workbook object workbook = Workbook() # Load an Excel workbook containing protected worksheet workbook.LoadFromFile("ProtectWorksheet.xlsx") # Get the first worksheet sheet = workbook.Worksheets[0] # Unprotect the worksheet using the specified password sheet.Unprotect("psd-permission") # Save the workbook to another Excel file workbook.SaveToFile("UnprotectWorksheet.xlsx", ExcelVersion.Version2016) workbook.Dispose()
Remove or Reset the Password of an Encrypted Workbook in Python
To remove or reset password of an encrypted workbook, you can use the Workbook.Unprotect() or the Workbook.Protect() method. The following steps show you how to load an encrypted Excel document and delete or change the password of it.
- Create a Workbook object.
- Specify the open password through Workbook.OpenPassword property.
- Load the encrypted Excel file using Workbook.LoadFromFile() method.
- Remove the encryption using Workbook.Unprotect() method. Or change the password using Workbook.Protect() method.
- Save the workbook to another Excel file using Workbook.SaveToFile() method.
- Python
from spire.xls import * from spire.xls.common import * # Create a Workbook object workbook = Workbook() # Specify the open password workbook.OpenPassword = "psd-123" # Load an encrypted Excel workbook workbook.LoadFromFile("Encrypted.xlsx") # Unprotect the workbook workbook.UnProtect() # Reset password # workbook.Protect("newpassword") # Save the workbook to another Excel file workbook.SaveToFile("UnprotectWorkbook.xlsx", ExcelVersion.Version2016) workbook.Dispose()
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.