7 Ways to Protect or Encrypt Excel Files in Python

Alice Yang
5 min readAug 1, 2023

--

Protect or Encrypt Excel Files in Python
Protect or Encrypt Excel Files in Python

Excel is a powerful tool used by millions of individuals and businesses to organize, analyze, and present data. Whether you use Excel for personal finances, project management, or data analysis, it is essential to protect your Excel files from unauthorized access and potential data breaches. In this article, we will demonstrate how to protect Excel files using Python.

We will discuss 7 different ways to protect Excel files:

Installation

To protect Excel files in Python, we will use Spire.XLS for Python, which is a feature-rich library for creating, reading, editing, and converting Excel files within Python applications.

You can install Spire.XLS for Python from pypi by running the following commands in your terminal:

pip install Spire.Xls

For more detailed information about the installation, you can check this official documentation: How to Install Spire.XLS for Python in VS Code.

Protect an Excel File with a Document-Open Password in Python

Password protection is the most common way to protect Excel files from unauthorized access. By setting a document-open password on an Excel file, you can prevent others from opening the file without your permission.

The following code snippet shows how to protect an Excel file with a document-open password in Python:

from spire.xls import *
from spire.xls.common import *

#Create a Workbook object
workbook = Workbook()
#Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

#Protect the Excel file with a document-open password
workbook.Protect("PasswordToOpen")

#Save the result file
workbook.SaveToFile("OpenPasswordProtected.xlsx", ExcelVersion.Version2013)
workbook.Dispose()

Protect an Excel File with a Document-Modify Password in Python

In addition to the document-open password, Excel allows you to set a separate password to modify it. By protecting an Excel file with a document-modify password, you restrict write access to specific users and ensure that the file can only be modified by authorized users.

When someone opens the protected file, Excel will require a document-modify password for write access. If he don’t have the password, he can only open the file as read-only and won’t be able to make any changes to the original file.

The following code snippet shows how to protect an Excel file with a document-modify password in Python:

from spire.xls import *
from spire.xls.common import *

#Create a Workbook object
workbook = Workbook()
#Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

#Protect the Excel file with a document-modify password
workbook.SetWriteProtectionPassword("PasswordToModify")

#Save the result file
workbook.SaveToFile("ModifyPasswordProtected.xlsx", ExcelVersion.Version2013)
workbook.Dispose()

Mark an Excel File as Final in Python

Marking an Excel file as final is another way to protect it from unintended modifications. This feature prevents users from making changes to the file’s content or formatting, making it useful when you want to share the file with others but don’t want them to accidentally modify or delete important data within it.

The following code snippet shows how to mark an Excel file as final in Python:

from spire.xls import *
from spire.xls.common import *

#Create a Workbook object
workbook = Workbook()
#Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

#Add a custom property to mark the document as final
workbook.CustomDocumentProperties.Add("_MarkAsFinal", True)

#Save the result file
workbook.SaveToFile("MarkAsFinal.xlsx", ExcelVersion.Version2013)
workbook.Dispose()

Protect an Excel Worksheet in Python

Excel also provides the option to restrict the editing and formatting of specific worksheets in an Excel file.

The following code snippet shows how to protect a specific worksheet in an Excel file in Python:

from spire.xls import *
from spire.xls.common import *

#Create a Workbook object
workbook = Workbook()
#Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

#Get the first worksheet
sheet = workbook.Worksheets[0]

#Protect the worksheet with a password and specify the protection type
sheet.Protect("SheetPassword", SheetProtectionType.none)

#Save the result file
workbook.SaveToFile("ProtectWorksheet.xlsx", ExcelVersion.Version2013)
workbook.Dispose()

Protect an Excel Worksheet while Allowing Certain Cell Ranges to be Edited in Python

If you want to allow specific cell ranges to be edited while protecting the rest of the worksheet, you can specify the editable cell ranges before protecting the worksheet.

The following code snippet shows how to protect an Excel worksheet while allowing certain cell ranges to be edited in Python:

from spire.xls import *
from spire.xls.common import *

#Create a Workbook object
workbook = Workbook()
#Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

#Get the first worksheet
sheet = workbook.Worksheets[0]

#Specify the editable cell range
sheet.AddAllowEditRange("EditableRange1", sheet.Range["B2:E11"])

#Protect the worksheet with a password
sheet.Protect("SheetPassword", SheetProtectionType.none)

#Save the result file
workbook.SaveToFile("AllowEditingRanges.xlsx", ExcelVersion.Version2013)
workbook.Dispose()

Protect Specific Cells or Ranges in an Excel Worksheet in Python

If you have formulas or important data in certain cells or ranges that you want to prevent from editing, you can lock those specific cells or ranges while leaving the rest of the worksheet editable.

The following code snippet shows how to lock specific cells or ranges in an Excel worksheet in Python:

from spire.xls import *
from spire.xls.common import *

#Create a Workbook object
workbook = Workbook()
#Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

#Get the first worksheet
sheet = workbook.Worksheets[0]

#Get the cells collection of the worksheet
cellsCollection = sheet.Cells

#Loop through the cells in the collection and unlock them (by default, the locked option is enabled for all cells)
for cell in cellsCollection:
cell.Style.Locked = False

#Lock specific cell range
sheet.Range["A1:E1"].Style.Locked = True

#Protect the worksheet with a password
sheet.Protect("SheetPassword", SheetProtectionType.none)

#Save the result file
workbook.SaveToFile("LockSpecificCells.xlsx", ExcelVersion.Version2013)
workbook.Dispose()

Protect Specific Rows or Columns in an Excel Worksheet in Python

If you have key information in certain rows or columns that you don’t want others to change, you can lock these rows or columns.

The following code snippet shows how to protect a specific row and column in an Excel worksheet in Python:

from spire.xls import *
from spire.xls.common import *

#Create a Workbook object
workbook = Workbook()
#Load an Excel file
workbook.LoadFromFile("Sample.xlsx")

#Get the first worksheet
sheet = workbook.Worksheets[0]

#Get the cells collection of the worksheet
cellsCollection = sheet.Cells

#Loop through the cells in the collection and unlock them (by default, the locked option is enabled for all cells)
for cell in cellsCollection:
cell.Style.Locked = False

#Lock the first row and column
sheet.Rows[0].Style.Locked = True
sheet.Columns[0].Style.Locked = True

#Protect the worksheet with a password
sheet.Protect("SheetPassword", SheetProtectionType.none)

#Save the result file
workbook.SaveToFile("LockSpecificRowAndColumn.xlsx", ExcelVersion.Version2013)
workbook.Dispose()

Conclusion

In this article, we have explored 7 different scenarios for protecting Excel files using Python. We hope you can find it helpful.

Related Topics

Convert Excel to Images (PNG, JPEG, BMP) with Python

Python — How to Convert Excel XLS or XLSX to PDF

Read Data from Excel Files in Python — A Comprehensive Guide

Merge Excel Workbooks or Worksheets in Python (Simple Example)

Quickly Delete or Remove Blank Rows, Columns or Cells from Excel with Python

Count the Number of Sheets, Rows, Columns and Cells in Excel in Python

--

--

Alice Yang

Skilled senior software developers with five years of experience in all phases of software development life cycle using .NET, Java and C++ languages.