Reputation: 53
I am using xlsxwriter to generate excel sheet. I am using worksheet.protect() to lock entire sheet. However, I want a group of cells to be unlocked. Also, I want those cell to be blank.
I tried to use worksheet.conditional_format(), but was unsuccessful. Please help.
I tried to use worksheet.write() to apply format(unlock) cell by cell, but it seems it requires a value to be provided. If I provide '' empty string, the unlock format was not applied, however if I provide a space ' ', I see the format(unlock) was applied successfully.
Upvotes: 1
Views: 3003
Reputation: 41644
In Excel or an XlsxWriter file, once the worksheet protection is enabled all cells are locked by default.
To turn off protection for a cell you need to add format with the lock
property turned off.
If you want a blank unlocked cell then use None
or the empty string ''
as the value to write()
or use write_blank()
explicitly, along with a format:
import xlsxwriter
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()
# Create a cell format with lock protection turned off.
unlocked = workbook.add_format({'locked': False})
# Format the columns to make the text more visible.
worksheet.set_column('A:A', 40)
# Turn worksheet protection on.
worksheet.protect()
# Write a locked and an unlocked cell.
worksheet.write('A1', 'Cell B1 is locked. It cannot be edited.')
worksheet.write('A2', 'Cell B2 is unlocked. It can be edited.')
worksheet.write('A3', 'Cell B3 is unlocked. It can be edited.')
worksheet.write('B1', 'Hello') # Locked by default.
worksheet.write('B2', None, unlocked)
worksheet.write_blank('B3', None, unlocked)
workbook.close()
Output:
Upvotes: 3