How To Set Cell Styles Using OpenPyX – Protection

Method

from openpyxl.styles import Protection

 

#Set protection

ws[‘C3’].protection=Protection(locked=True)

ws[‘C3′]=’abc123’

ws.protection.sheet=True

Sample Code

#Cell Style: Setting Protection

#Import load_workbook function
from openpyxl import Workbook

#Create a new workbook
wb=Workbook()
#Get the active worksheet
ws=wb.active

#Import Protection class
from openpyxl.styles import Protection

#Set protection
ws['C3'].protection=Protection(locked=False)
ws['C3']='abc123'
ws.protection.sheet=True

wb.save('test.xlsx')
wb.close()

Leave a Reply

Your email address will not be published. Required fields are marked *