Lock Cells to Protect Them with Python.NET
Possible Usage Scenarios
Locking cells to protect them is a common practice in spreadsheet applications, such as Microsoft Excel or Google Sheets, for several important reasons:
- Preventing Accidental Changes: Locking cells can prevent users from accidentally modifying important data or formulas.
- Maintaining Data Integrity: Ensure critical data remains consistent and accurate.
- Controlled Access: Manage editing permissions in collaborative environments.
- Protecting Formulas: Safeguard crucial calculations from alteration.
- Enforcing Business Rules: Comply with data protection requirements.
- Guiding Users: Provide clear editable areas in complex spreadsheets.
How to Lock Cells to Protect Them in Excel
Here’s how you can lock cells in Microsoft Excel:
- Select the Cells to Lock: Choose cells or skip to lock entire sheet.
- Open the Format Cells Dialog: Right-click > “Format Cells” or Ctrl+1.
- Lock the Cells: Go to “Protection” tab > Check “Locked” > Click “OK.”
- Protect the Worksheet: “Review” tab > “Protect Sheet” > Set password/permissions > Click “OK.”
How to Lock Cells to Protect Them Using Python
Aspose.Cells for Python via .NET enables programmatic cell protection. Follow these steps:
- Load sample file
- Unlock all cells (default locked state is not enforced until protection)
- Lock specific cells
- Protect worksheet to enforce locking
import aspose.cells as ac
# Load sample workbook
workbook = ac.Workbook("sample.xlsx")
worksheet = workbook.worksheets[0]
# Unlock all cells first
style = ac.Style()
style.is_locked = False
style_flag = ac.StyleFlag()
style_flag.locked = True
worksheet.cells.apply_style(style, style_flag)
# Lock specific cells
worksheet.cells["A1"].get_style().is_locked = True
worksheet.cells["B2"].get_style().is_locked = True
# Enable worksheet protection
worksheet.protect(ac.ProtectionType.ALL)
# Save protected workbook
workbook.save("output.xlsx")
Output Result
This implementation locks specified cells (A1 and B2) while keeping others editable. Worksheet protection enforces these settings.

from aspose.cells import Workbook, ProtectionType, StyleFlag
# Load the Excel file
workbook = Workbook("sample.xlsx")
# Access the first worksheet
sheet = workbook.worksheets[0]
# Unlock all cells first
unlock_style = workbook.create_style()
unlock_style.is_locked = False
style_flag = StyleFlag()
style_flag.locked = True
sheet.cells.apply_style(unlock_style, style_flag)
# Lock specific cells (A1 and B2)
lock_style = workbook.create_style()
lock_style.is_locked = True
sheet.cells.get("A1").set_style(lock_style)
sheet.cells.get("B2").set_style(lock_style)
# Protect the worksheet to enforce locking
sheet.protect(ProtectionType.ALL)
# Save the modified workbook
workbook.save("output_locked.xlsx")