Protect Excel Files in Python

MS Excel provides a quick and easy way to store small as well as large-sized data in the form of rows and columns. In addition, it lets you perform various other operations such as data formatting, computations, data analysis, graphs and charts, and so on. Since data is worth being protected, MS Excel allows you to password-protect the Excel files. In this article, you will learn how to password-protect Excel files in Python. Moreover, we will demonstrate how to protect a specific Excel worksheet and unprotect Excel files dynamically in Python.

Python Library to Protect Excel Files - Free Download

Aspose.Cells for Python is a powerful and feature-rich library, which is designed to create, edit, and convert Excel files from within the Python applications. We will use this library to protect and unprotect Excel workbooks or worksheet. You can either download its package or install it from PyPI using the following command.

pip install aspose-cells

Simple Steps to Protect Excel Files in Python

First, let’s have a look at the protection types that Aspose.Cells for Python provides to protect the Excel files.

  • 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 saved structure
  • WINDOWS - User cannot modify saved windows
  • NONE - No protection

You can apply any of the above-mentioned protection types to your Excel files. The following are the simple steps to protect an Excel file with Aspose.Cells for Python.

  • Load Excel file from desired disk location.
  • Apply required protection on the file and set password.
  • Save the protected file.

Password-Protect Excel Files in Python

Now, let’s have a look at how to password-protect an Excel file using Python code. The following are the steps to do so.

  • Load Excel file using Workbook class.
  • Use Workbook.protect(ProtectionType, password) method to protect the Excel file with password.
  • Save the protected file using Workbook.save(fileName) method.

The following code sample shows how to password-protect an Excel file in Python.

Unprotect Excel Files in Python

You can also unprotect a protected Excel file quite easily. Simply load the Excel file, unprotect it using the password, and save the updated file to desired location.

The following are the steps to unprotect a password-protected Excel file in Python.

  • Load Excel file using Workbook class.
  • Use Workbook.unprotect(password) method to unprotect the Excel file.
  • Save the unprotected Excel file using Workbook.save(fileName) method.

The following code sample shows how to unprotect an Excel file in Python.

Python: Apply Password-Protection to an Excel Worksheet

You can also apply protection to an Excel file at worksheet level. The following are the steps to protect a specific Excel worksheet with password in Python.

  • Load Excel file using Workbook class.
  • Get reference of the desired worksheet in an object using Workbook.getWorksheets().get(index) method.
  • Apply desired protection type using Worksheet.protect(ProtectionType) method.
  • Set password using Worksheet.getProtection().setPassword(password) method.
  • Save the Excel file using Workbook.save(fileName) method.

The following code sample shows how to protect an Excel worksheet in Python.

Unprotect Excel Worksheets in Python

You can unprotect a protected Excel sheet in a quite similar way. The only difference is, you will use the Worksheet.unprotect(password) method.

The following code sample shows how to unprotect an Excel worksheet in Python.

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

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

Conclusion

In this article, you have learned how to password-protect and unprotect Excel files in Python. In addition, you have seen how to protect and unprotect a particular worksheet in an Excel file. You can also explore more about the Python Excel automation API using documentation. In case you would have any queries, contact us via our forum.

See Also