Protect Excel Files in Python

MS Excel provides a quick and easy way to store small as well as large-sized data in rows and columns. It also lets you format data, perform calculations, analyze data, and create graphs and charts. Because data needs protection, Excel allows you to password‑protect files. In this article you will learn how to password‑protect Excel files in Python and how to protect a specific worksheet and unprotect Excel files dynamically.

Python Library to Protect Excel Files - Free Download

Aspose.Cells for Python is a powerful, feature‑rich library designed to create, edit, and convert Excel files from Python applications. We will use this library to protect and unprotect workbooks or worksheets. You can either download the package or install it from PyPI with the command:

pip install aspose-cells

Simple Steps to Protect Excel Files in Python

First, review the protection types Aspose.Cells for Python offers:

  • ALL – User cannot modify anything
  • CONTENTS – User cannot enter data
  • OBJECTS – User cannot modify drawing objects
  • SCENARIOS – User cannot modify saved scenarios
  • STRUCTURE – User cannot modify workbook structure
  • WINDOWS – User cannot modify window settings
  • NONE – No protection

You can apply any of these types to your Excel files. Follow these simple steps:

  1. Load the Excel file from the desired location.
  2. Apply the required protection and set a password.
  3. Save the protected file.

Password-Protect Excel Files in Python

To password‑protect an Excel file, follow these steps:

  1. Load the file using the Workbook class.
  2. Call Workbook.protect(ProtectionType, password) to set protection.
  3. Save the file with Workbook.save(fileName).

The code sample below demonstrates how to protect a workbook:

Unprotect Excel Files in Python

Unprotecting a workbook is straightforward. Load the file, remove protection with the password, and save the result.

  1. Load the file using Workbook.
  2. Call Workbook.unprotect(password).
  3. Save the unprotected file with Workbook.save(fileName).

The following code shows how to unprotect a workbook:

Python: Apply Password-Protection to an Excel Worksheet

You can also protect a specific worksheet:

  1. Load the workbook with Workbook.
  2. Get the worksheet using Workbook.getWorksheets().get(index).
  3. Apply protection with Worksheet.protect(ProtectionType).
  4. Set a password via Worksheet.getProtection().setPassword(password).
  5. Save the workbook with Workbook.save(fileName).

Code example:

Unprotect Excel Worksheets in Python

To unprotect a worksheet, use the Worksheet.unprotect(password) method.

Python Library to Password-Protect Excel Files - Get a Free License

You can get a free temporary license to use Aspose.Cells for Python without evaluation limits.

Conclusion

You have learned how to password‑protect and unprotect Excel workbooks and worksheets in Python using Aspose.Cells. Explore more of the Python Excel automation API in the documentation. For questions, visit our forum.

See Also