Python excel protection.set_password error

Good Morning,
I am building a script which removes the read only property from a spreadsheet, opens the spreadsheet, removes the sheet protection, makes a change, readd the sheet protection and reapplies the read only property. However i keep getting an error on the protection.set_password part. The error i am getting is “TypeError: ‘bool’ object is not callable”

The code i am using is below. Does anyone know what is causing the error? as i am not 100% what the error means

import os
import stat
import openpyxl as op
from openpyxl import load_workbook

#Open workbook and check if FRN exists
file_name = r'c:\test.xlsx'
os.chmod(file_name, stat.S_IWRITE)
wb = op.load_workbook(file_name)
ws = wb.active
sheet = wb["Sheet1"]
sheet.protection.set_password = False
ws["A1"] = "TestValue"
pword = 'passw0rd11'
wb["Sheet1"].protection.set_password(pword)
wb.save(r"c:\test1.xlsx") 
wb.close
os.chmod(file_name, stat.S_IREAD)

Hi,

it doesn’t appear that you need this line since you are already getting access to it via tha op prefix.

from openpyxl import load_workbook

The set_password property is a Boolean value. It’s not a method that can be called. I would imagine (but haven’t worked with this) that doing:

sheet.protection.set_password = False

would restore the password to what it had been.

Thank you so much, it is working fine now.

1 Like