Python Forum
[openpyxl] Set the locking property of all cells
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[openpyxl] Set the locking property of all cells
#1
Question 
I'm dealing with cell locking (unlocking) using openpyxl module.

What I need to do is to set the locking property of all cells of a workbook. The openpyxl "standard" iterators are not going throught all the cells (only the ones that have values - not the whole workbook), and forcing that using the iter_rows() method is leading to time and memory issues.

import openpyxl

def reset_locking(file):

	workbook = openpyxl.load_workbook(file)
	
	# first implementation
	for sheet in workbook:
            for row in sheet.rows:
                for cell in row:
                    cell.protection = openpyxl.styles.Protection(locked = True)
	
	#second implementation
	#max data are related to Exel version >= 2016
	for sheet in workbook:
            for row in sheet.iter_rows(min_row=1, min_col=1, max_row=1048576, max_col=16384):
                for cell in row:
                    cell.protection = openpyxl.styles.Protection(locked = True)
Is there a different (or smarter) way to do it?
Maybe defining a style and applying it to all the cellls could help?

Based on what I understood from openpyxl documentation, seems that this approach (modifying property or styles of a cell range in one shot) is not feasible... but maybe I missed something.

Or are there any different modules that I could use?

Any kind of help is really appreciated. Thanks.

BelleroDev
Reply
#2
By default Lock property of a Cell is set to True (checked in Excel)
Unless it was set to False before that why do you need to iterate over every cell? Or am I missing something in your question?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Nov-07-2025, 09:57 AM)buran Wrote: By default Lock property of a Cell is set to True (checked in Excel)
Unless it was set to False before that why do you need to iterate over every cell? Or am I missing something in your question?

Yes, you're right. But I need to process excel files where the locking status of some cells have been already changed to False.
My application needs first to reset the cell locking to True and than do the other stuff. That preliminary step of resetting can be done also manually in Excel before running my app (it will be not too much effort for my users), but I was trying to find a way to do it automatically.
Reply
#4
Quote:The openpyxl "standard" iterators are not going throught [sic] all the cells (only the ones that have values - not the whole workbook)

Why do you want to lock cells that do not have any value in them?

Post a sample of your XL file. What are you trying to do?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Openpyxl-change value of cells in column based on value that currently occupies cells phillipaj1391 5 22,544 Mar-30-2022, 11:05 PM
Last Post: Pedroski55
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 5,617 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  Mult-threading and locking file mr_byte31 4 5,806 Oct-16-2021, 01:54 AM
Last Post: Larz60+
  ABC Module and @property decorator, Pythonic Way? muzikman 21 12,389 Aug-18-2021, 06:08 PM
Last Post: muzikman
  @property vs __set__ / __get__ and __setattr__ / __getattr__ okhajut 1 6,912 Jun-15-2021, 03:48 PM
Last Post: snippsat
  How can I iterate through all cells in a column (with merge cells) with openpyxl? aquerci 1 10,460 Feb-11-2021, 09:31 PM
Last Post: nilamo
  Can you help me to merge the cells with OpenPyXL? TurboC 1 3,632 Feb-01-2021, 12:54 AM
Last Post: Larz60+
  Can property getters and setters have additional arguments? pjfarley3 2 5,187 Oct-30-2020, 12:17 AM
Last Post: pjfarley3
  [openpyxl] Increment cells being pasted into Template Kristenl2784 4 5,606 Jul-16-2020, 10:00 PM
Last Post: Kristenl2784
  Property price calculation oli_action 4 5,478 Jul-15-2020, 04:27 PM
Last Post: sridhar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020