如何使用 openpyxl 保护 excel 中的列免受更改?
How to protect a column in excel against changes using openpyxl?
我想锁定 for changes 中的一些列。为此,我首先锁定了整个文件。
ws = wb["RFI"]
ws.protection.sheet = True
但后来我尝试锁定一些列
for col in ['U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD']:
ws[col].protection = Protection(locked=False)
我得到了
AttributeError: 'tuple' object has no attribute 'protection'
当您使用 worksheet['<some letter>']
时,您得到的是该列中所有单元格的元组:
ws['A'] -> (Cell A1, Cell A2 ... Cell A<max_row>)
锁定 sheet 后,您需要遍历要解锁的单元格:
ws = wb["RFI"]
ws.protection.sheet = True
for col in ['U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD']:
for cell in ws[col]:
cell.protection = Protection(locked=False)
这样您的 sheet 将被锁定,但那些特定的列不会被锁定。
我想锁定 for changes 中的一些列。为此,我首先锁定了整个文件。
ws = wb["RFI"]
ws.protection.sheet = True
但后来我尝试锁定一些列
for col in ['U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD']:
ws[col].protection = Protection(locked=False)
我得到了
AttributeError: 'tuple' object has no attribute 'protection'
当您使用 worksheet['<some letter>']
时,您得到的是该列中所有单元格的元组:
ws['A'] -> (Cell A1, Cell A2 ... Cell A<max_row>)
锁定 sheet 后,您需要遍历要解锁的单元格:
ws = wb["RFI"]
ws.protection.sheet = True
for col in ['U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD']:
for cell in ws[col]:
cell.protection = Protection(locked=False)
这样您的 sheet 将被锁定,但那些特定的列不会被锁定。