Cell protection using openpyxl

1,027 views
Skip to first unread message

Anchal Kapoor

unread,
Jul 21, 2014, 4:27:51 AM7/21/14
to openpyx...@googlegroups.com
We can enable worksheet protection as follows:
worksheet.protection.enable()
is there some way that we can protect cells of only some specific columns using openpyxl?

Charlie Clark

unread,
Jul 21, 2014, 4:58:52 AM7/21/14
to openpyx...@googlegroups.com
"protection" can only be applied to individual cells. When a worksheet is
protected then, by default, all cells in the worksheet are protected. It
is, however, possible to remove the protection for individual cells.

Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel: +49-211-600-3657
Mobile: +49-178-782-6226

Anchal Kapoor

unread,
Jul 21, 2014, 6:00:48 AM7/21/14
to openpyx...@googlegroups.com
Thanks for the help Charlie.

I have tried a lot of things, can you please provide more information as to how we can remove protection for individual cells?

Charlie Clark

unread,
Jul 21, 2014, 7:07:24 AM7/21/14
to openpyx...@googlegroups.com
Am .07.2014, 12:00 Uhr, schrieb Anchal Kapoor <anch...@gmail.com>:

> I have tried a lot of things, can you please provide more information as
> to
> how we can remove protection for individual cells?

Not sure if it's actually working as expected at the moment.

See
https://bitbucket.org/openpyxl/openpyxl/issue/339/explicitly-unlocked-cells-are-still-marked

Anchal Kapoor

unread,
Jul 22, 2014, 2:01:18 AM7/22/14
to openpyx...@googlegroups.com
Thanks Charlie.

vishal lad

unread,
2:37 AM (5 hours ago) 2:37 AM
to openpyxl-users
I did achieve it by

from openpyxl import Workbook
from openpyxl.styles import Protection

wb = Workbook()
ws = wb.active

# Write the formula
ws['A1'] = '=HYPERLINK("https://google.com", "Click Here")'

# Apply the protection setting to the specific cell
# hidden=True: Hides formula from formula bar
# locked=True: Prevents users from editing the cell
ws['A1'].protection = Protection(hidden=True, locked=True)

# CRITICAL: This setting does nothing until protection is enabled
ws.protection.sheet = True
ws.protection.password = "your_password" # Optional

wb.save("hidden_formula.xlsx")
Reply all
Reply to author
Forward
0 new messages