IRONSOFTWAREHOME
Protect Excel Files
from ironxl import *

# Load the Excel workbook
workbook = WorkBook.Load("sample.xlsx")
worksheet = workbook.DefaultWorkSheet

# Open a protected spreadsheet file
protected_workbook = WorkBook.Load("sample.xlsx", "IronSoftware")

# Spreadsheet protection
# Set protection for the spreadsheet file
workbook.Encrypt("IronSoftware")

# Remove protection for the spreadsheet file. Original password is not required.
workbook.Password = None

workbook.Save()

# Worksheet protection
# Set protection for an individual worksheet
worksheet.ProtectSheet("IronXL")

# Remove protection for a particular worksheet. It works without a password!
worksheet.UnprotectSheet()

workbook.Save()
pip install ironxl
Protect Excel Files

Protect Excel Files

The Encrypt method in IronXL gives Python developers the ability to apply password protection to both entire Excel workbooks and individual worksheets. This is essential for safeguarding sensitive data and ensuring that only authorized users can open or modify Excel files generated by your Python application.

IronXL also supports sheet-level protection, which prevents users from editing specific worksheets while still allowing them to view or interact with data. This is particularly useful in collaborative environments where write access needs to be restricted. Protection can be added or removed programmatically, providing flexibility for both secure and temporarily accessible workflows.

WorkBook.Load opens the existing Excel file from disk. Once loaded, the Encrypt method applies a password at the workbook level, preventing the file from being opened without the correct credentials.

The DefaultWorkSheet property retrieves the primary worksheet. Calling ProtectSheet on it adds a second layer of security at the sheet level, restricting edits even for users who can open the workbook.

Finally, Save persists both the workbook-level encryption and the sheet-level protection, so the security settings are preserved in the output file. This two-level approach covers both read access and edit access control in a single workflow.

Learn How to Set Passwords on Excel Workbooks in Python

Ready to Get Started?

Version:2026.9just released

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required