[Comprehensive Guide] How to Password Protect an Excel File or Unprotect it in Python
![[Comprehensive Guide] How to Password Protect an Excel File or Unprotect it in Python](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1729135417177%2F0f33b8a5-6bfe-4cd2-915d-205343c840d0.png&w=3840&q=75)
If your Excel document contains confidential information or you want to prevent others from editing it freely, encrypting the file is the best solution. By password-protecting an Excel file, you safeguard important data while allowing others to view it without making unauthorized changes, which is especially useful for sensitive documents like invoices or budget statements.
While MS Excel offers built-in encryption, handling multiple files manually can be time-consuming. In this article, you'll find a faster way. You will learn how to password protect an Excel file or unprotect it using Python, streamline your workflow and boost productivity.
Python Library to Encrypt Excel Files
To finish the task, in today’s guide, we recommend Spire.XLS for Python. It is an easy-to-use and powerful Python library that allows users to create, edit, convert Excel documents, and so on. This tool enables developers to process Excel files with ease, no matter if they are prone or experts.
You can install it using the PyPI command: pip install Spire.XLS.
How to Password Protect an Excel File with 3 Steps
There are several types to password protect an Excel file. If you want to secure the entire Excel file, you can use the Workbook.Protect() method offered by Spire.XLS. This ensures that the whole file is encrypted, preventing unauthorized access. Without the correct password, others won’t be able to open or read the file, offering the highest level of protection for your Excel document.
3 Steps to protect an Excel file with a password:
Create an object of the Workbook class, and load an Excel document from a file folder using the Workbook.LoadFromFile() method.
Password protects the Excel file by calling the Workbook.Protect() method.
Save the encrypted file as a new Excel document with the Workbook.SaveToFile() method. Then release the resources.
Here is the code example of setting a password to protect an Excel document:
from spire.xls import *
from spire.xls.common import *
# Create a Workbook object
workbook = Workbook()
# Load an Excel workbook from file
workbook.LoadFromFile("/sample.xlsx")
# Protect the workbook with a password
workbook.Protect("password-123")
# Save the workbook as a new Excel file
workbook.SaveToFile("/Encrypted.xlsx", ExcelVersion.Version2016)
# Release the memory
workbook.Dispose()
Clarify the Protection Types of Excel and Customize Editing Options
Directly blocking unauthorized access like opening or viewing is the quickest and safest way. However, sometimes more flexible protection is needed. For example, you might have to allow customers to edit ranges to confirm or change their own information in a document. You might need to empower team members to add rows and columns in a shared Excel file.
This section outlines the different protection options Spire offers for Excel worksheets, so you can choose the one that best fits your needs.
Table of Protection Types and Their Definitions
| 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 nothing on the protected worksheet. |
| none | Do any operations listed above on the protected worksheet. |
To apply these protection types, you only need to set the SheetProtectionType parameters. For example:
# Protect the worksheet with a password and a specific protection type
worksheet.Protect("password-permission", SheetProtectionType.InsertingRows)
Allow Others to Edit Certain Ranges in Password-Protected Excel Files
Building on the table above, this section covers one of the most common scenarios when editing a protected Excel document—allowing specific people to edit designated cell areas.
Steps to allow editing of specific cell areas:
Create an instance of the Workbook class, and specify the file path for loading an Excel document using the Workbook.LoadFromFile() method.
Retrieve a specified worksheet with the Workbook.Worksheets[] method.
Configure the ranges that are allowed to be edited by calling the Worksheet.AddAllowEditRange() method.
Password protect the Excel file with the Worksheet.Protect() method.
Write the protected document to the disk using the Workbook.SaveToFile() method. Then release the resources.
Below is an example of adding editable ranges to a password-protected Excel document:
from spire.xls import *
from spire.xls.common import *
# Create a Workbook object
workbook = Workbook()
# Load an Excel workbook from file
workbook.LoadFromFile("/sample.xlsx")
# Get the second worksheet
sheet = workbook.Worksheets[1]
# Add ranges that allow editing
sheet.AddAllowEditRange("Range One", sheet.Range["B3:K17"])
# Protect the worksheet with a password and set the protection type
sheet.Protect("password-permission", SheetProtectionType.All)
# Write the Excel file to disk
workbook.SaveToFile("/AllowEditRange.xlsx", ExcelVersion.Version2016)
# Release the resources
workbook.Dispose()
How to Unprotect an Excel File in Python: 4 Steps
After learning adding an open password and permission password to an Excel file, it is time to check out decrypting an Excel file. Once the data is no longer confidential, unprotecting the Excel file can make the document accessible to everyone.
Steps to unprotect an Excel file:
Instantiate a Workbook class, and read a password-protected Excel file using the Workbook.LoadFromFile() method.
Get a certain worksheet with the Workbook.Worksheets[] method.
Decrypt the Excel spreadsheet by calling the Worksheets.Unprotect() method.
Save the updated document as a new file using the Workbook.SaveToFile() method and release resources.
Here is an example of unprotecting an Excel file that is password protected:
from spire.xls import *
from spire.xls.common import *
# Create a Workbook object
workbook = Workbook()
# Load an Excel workbook from file
workbook.LoadFromFile("/AllowEditRange.xlsx")
# Get the second worksheet
sheet = workbook.Worksheets[1]
# Unprotect the worksheet
sheet.Unprotect("password-permission")
# Save the decrypted Excel file
workbook.SaveToFile("/UnprotectWorksheet.xlsx", ExcelVersion.Version2016)
# Release resources
workbook.Dispose()
The Bottom Line
The article illustrates how to password-protect an Excel file in Python and the way to unprotect it. Password protection is divided into the highest level of "Open Password" and various types of permission passwords. Each section includes detailed steps and code examples to guide you. By the end of this article, you'll be able to easily manage Excel documents and safeguard confidential information.