Given that an Excel file contains some cells protected with passwords, I want to detect these protected cells to choose whether to include them in the inputs or skip them.
I have tried pandas
df = pd.read_excel('test.xlsx', engine='openpyxl')
and openpyxl
wb = openpyxl.load_workbook('test.xlsx') sheet = wb['Sheet1'] # B4 is a protected cell with a specific password so I cannot change its value in # the Excel file without the password print(sheet['B4'].value) >> 8 sheet['B4'].value = 7 print(sheet['B4'].value) >> 7
However, the protected cells are read normally like other unprotected cells and could be easily changed.
So the question is, how could I detect these protected cells?
For example, is there any way to read the attributes of the Excel file that indicate if the cell is protected or not?
Advertisement
Answer
With openpyxl, you can detect the locked or hidden status of each cell with its protection
attribute, e.g.:
>>> sheet['B4'].protection.locked True >>> sheet['B4'].protection.hidden False