2016-11-15 58 views
1

我使用xlsxwriter生成excel表單。我正在使用worksheet.protect()來鎖定整個工作表。不過,我想要解鎖一組單元格。另外,我希望這些單元格是空白的。xlswriter寫入解鎖格式的空白單元

我試圖使用worksheet.conditional_format(),但不成功。請幫忙。

我試圖使用worksheet.write()逐個單元格應用格式(解鎖),但它似乎需要一個值來提供。如果我提供''空字符串,解鎖格式不適用,但是如果我提供空格'',則看到格式(解鎖)已成功應用。

回答

1

在Excel或XlsxWriter文件中,一旦啓用了工作表保護,所有單元默認都被鎖定。

要關閉對單元格的保護,您需要添加關閉lock屬性的格式。

如果你想有一個空手機解鎖,然後使用None或空字符串''作爲價值write()或用格式使用write_blank()明確,沿着:

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() 

輸出

enter image description here

+0

非常感謝。 – user3117282