SoFunction
Updated on 2025-04-27

7 Ways to Protect or Encrypt Excel Files with Python

introduction

Excel files usually contain sensitive information, such as financial records, customer data, or proprietary formulas. Protecting and encrypting these files is essential to prevent unauthorized access and maintain confidentiality. In this article, we will explore howusePythonAutomated ExcelFile security protection and encryption process, ensure the security of the data. It mainly includes the following topics:

  • Open password-protected Excel files with documents
  • Use documents to modify password to protect Excel files
  • Mark Excel files as final version
  • Protect Excel worksheets
  • Allows editing of certain cells while protecting Excel sheets
  • Locking specific cells in an Excel worksheet
  • Lock a specific row or column in an Excel worksheet

Install Python Excel Library

In Python, we can use the for Python library to protect or encrypt Excel files. This library provides multiple functions to protect and encrypt Excel files, such as setting passwords, applying worksheet protection, marking Excel as final version, locking cells, rows, columns, and more.

You can install for Python from PyPI using the following pip command:

pip install 

If you are not sure how to install it, check out this document:Operation Guide for installing for Python in Windows_python_me

Python Open password-protected Excel files with documents

The main function of a document opening password is to restrict unauthorized people from opening or accessing documents. Once the password is set, anyone needs to enter the correct password to successfully open the Excel file.

By using for Python()Method: You can set the document opening password for Excel file. The key steps are as follows:

  • createWorkbookAn instance of the class.
  • use()Method loads Excel file.
  • use()Method set the document opening password for the Excel file.
  • use()Method saves the result file.

Complete code:

from  import *
from  import *
 
# Create a Workbook objectworkbook = Workbook()
# Load an Excel file("Test.xlsx")
 
# Set the document to open the password("12345Open")
 
# Save the result file("Document Open Password.xlsx", ExcelVersion.Version2013)
()

It should be noted that the document opening password and modifying password can be set separately or at the same time. When these two passwords are set at the same time, the user not only needs to enter the correct opening password to open the file, but also needs to enter the correct modification password to edit the file content.

Python marks Excel files as final version

When an Excel file is marked as the final version, other users will see a prompt "This file has been marked as the final version" when opening the file, knowing that it should not be edited again. This method can effectively prevent Excel files from being modified unexpectedly.

To mark Excel files as final version, you can use("_MarkAsFinal", True)method. The specific steps are as follows:

  • createWorkbookAn instance of the class.
  • use()Method loads Excel file.
  • use("_MarkAsFinal", True)Method marks the Excel file as the final version.
  • use()Method saves the result file.

Complete code:

from  import *
from  import *
 
# Create a Workbook objectworkbook = Workbook()
# Load an Excel file("Test.xlsx")
 
# Mark the document as the final version("_MarkAsFinal", True)
 
# Save the result file("Final Version.xlsx", ExcelVersion.Version2013)
()

Python protects Excel worksheets

In addition to protecting the entire Excel file, Excel also supports protection of individual worksheets, thereby limiting users' editing operations on specific worksheets.

To protect a specific Excel worksheet, you can use()method. The specific steps are as follows:

  • createWorkbookAn instance of the class.
  • use()Method loads Excel file.
  • use[index]Properties get a specific worksheet.
  • use()Method to protect this worksheet.
  • use()Method saves the result file.

Complete code:

from  import *
from  import *
 
# Create a Workbook objectworkbook = Workbook()
# Load an Excel file("Test.xlsx")
 
# Get the first worksheetsheet = [0]
 
# Password protect the worksheet and specify the protection type("Sheet111", )
 
# Save the result file("Protect Worksheet.xlsx", ExcelVersion.Version2013)
()

Python allows editing of certain cells while protecting Excel sheets

When protecting an Excel worksheet, you can set an editable area to allow users to edit certain specific cells in the protected worksheet.

To set up editable areas of the worksheet, you can use()method. The specific steps are as follows:

  • createWorkbookAn instance of the class.
  • use()Method loads Excel file.
  • use[index]Properties get a specific worksheet.
  • use()Method specifies the editable range of cells after protecting the worksheet.
  • use()Method to protect this worksheet.
  • use()Method saves the result file.

Complete code:

from  import *
from  import *
 
# Create a Workbook objectworkbook = Workbook()
# Load an Excel file("Test.xlsx")
 
# Get the first worksheetsheet = [0]
 
# Specify an editable cell area("Range1", ["B2:E11"])
 
# Password protect worksheets("Sheet111", )
 
# Save the result file("Editable Area.xlsx", ExcelVersion.Version2013)
()

Python locks specific cells in Excel worksheets

If some of the key cells in an Excel worksheet contain important formulas or data, you can lock and protect them. This prevents normal users from editing these key cells while still allowing them to operate on other cells.

To lock a specific cell, you can use[].property. The specific steps are as follows:

  • createWorkbookAn instance of the class.
  • use()Method loads Excel file.
  • use[index]Properties get a specific worksheet.
  • useProperties get the collection of cells for the worksheet.
  • Loop through the cells and cancel all cells' locked states (the locked states of all cells in Excel are selected by default).
  • use[].Properties lock specific cells or ranges of cells.
  • use()Method to protect this worksheet.
  • use()Method saves the result file.

Complete code:

from  import *
from  import *
 
# Create a Workbook objectworkbook = Workbook()
# Load an Excel file("Test.xlsx")
 
# Get the first worksheetsheet = [0]
 
# Get the worksheet's cell collectioncellsCollection = 
 
# Loop through cells and cancel the lock status of all cells (in Excel, the lock status of all cells is selected by default)for cell in cellsCollection:
     = False
 
# Lock specific cell areas["A1:E1"]. = True
 
# Password protect worksheets("Sheet111", )
 
# Save the result file("Lock cell.xlsx", ExcelVersion.Version2013)
()

Python locks a specific row or column in an Excel worksheet

In addition to locking specific cells or regions, you can also lock entire rows or columns.

To lock a specific row or column, you can use[index].or[index].property. The specific steps are as follows:

  • createWorkbookAn instance of the class.
  • use()Method loads Excel file.
  • use[index]Properties get a specific worksheet.
  • useProperties get the collection of cells for the worksheet.
  • Loop through the cells and cancel all cells' locked states (the locked states of all cells in Excel are selected by default).
  • use[index].or[index].The property locks a specific row or column.
  • use()Method to protect this worksheet.
  • use()Method saves the result file.

Complete code:

from  import *
from  import *
 
# Create a Workbook objectworkbook = Workbook()
# Load an Excel file("Test.xlsx")
 
# Get the first worksheetsheet = [0]
 
# Get the worksheet's cell collectioncellsCollection = 
 
# Loop through cells and cancel the lock status of all cells (in Excel, the lock status of all cells is selected by default)for cell in cellsCollection:
     = False
 
# Lock the first row and the first column[0]. = True
[0]. = True
 
# Password protect worksheets("Sheet111", )
 
# Save the result file("Locking ranks.xlsx", ExcelVersion.Version2013)
()

The above are 7 different scenarios for using Python to protect or encrypt Excel files. Hope it helps you.

This is the end of this article about 7 ways to protect or encrypt Excel files using Python. For more related contents of Python protection or encrypting Excel files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!