Source code

Download source archive ↓

src/openpyxl/worksheet/protection.py

1# Copyright (c) 2010-2024 openpyxl
2
3from openpyxl.descriptors import (
4    Bool,
5    String,
6    Alias,
7    Integer,
8)
9from openpyxl.descriptors.serialisable import Serialisable
10from openpyxl.descriptors.excel import (
11    Base64Binary,
12)
13from openpyxl.utils.protection import hash_password
14
15
16class _Protected:
17    _password = None
18
19    def set_password(self, value='', already_hashed=False):
20        """Set a password on this sheet."""
21        if not already_hashed:
22            value = hash_password(value)
23        self._password = value
24
25    @property
26    def password(self):
27        """Return the password value, regardless of hash."""
28        return self._password
29
30    @password.setter
31    def password(self, value):
32        """Set a password directly, forcing a hash step."""
33        self.set_password(value)
34
35
36class SheetProtection(Serialisable, _Protected):
37    """
38    Information about protection of various aspects of a sheet. True values
39    mean that protection for the object or action is active This is the
40    **default** when protection is active, ie. users cannot do something
41    """
42
43    tagname = "sheetProtection"
44
45    sheet = Bool()
46    enabled = Alias('sheet')
47    objects = Bool()
48    scenarios = Bool()
49    formatCells = Bool()
50    formatColumns = Bool()
51    formatRows = Bool()
52    insertColumns = Bool()
53    insertRows = Bool()
54    insertHyperlinks = Bool()
55    deleteColumns = Bool()
56    deleteRows = Bool()
57    selectLockedCells = Bool()
58    selectUnlockedCells = Bool()
59    sort = Bool()
60    autoFilter = Bool()
61    pivotTables = Bool()
62    saltValue = Base64Binary(allow_none=True)
63    spinCount = Integer(allow_none=True)
64    algorithmName = String(allow_none=True)
65    hashValue = Base64Binary(allow_none=True)
66
67
68    __attrs__ = ('selectLockedCells', 'selectUnlockedCells', 'algorithmName',
69              'sheet', 'objects', 'insertRows', 'insertHyperlinks', 'autoFilter',
70              'scenarios', 'formatColumns', 'deleteColumns', 'insertColumns',
71              'pivotTables', 'deleteRows', 'formatCells', 'saltValue', 'formatRows',
72              'sort', 'spinCount', 'password', 'hashValue')
73
74
75    def __init__(self, sheet=False, objects=False, scenarios=False,
76                 formatCells=True, formatRows=True, formatColumns=True,
77                 insertColumns=True, insertRows=True, insertHyperlinks=True,
78                 deleteColumns=True, deleteRows=True, selectLockedCells=False,
79                 selectUnlockedCells=False, sort=True, autoFilter=True, pivotTables=True,
80                 password=None, algorithmName=None, saltValue=None, spinCount=None, hashValue=None):
81        self.sheet = sheet
82        self.objects = objects
83        self.scenarios = scenarios
84        self.formatCells = formatCells
85        self.formatColumns = formatColumns
86        self.formatRows = formatRows
87        self.insertColumns = insertColumns
88        self.insertRows = insertRows
89        self.insertHyperlinks = insertHyperlinks
90        self.deleteColumns = deleteColumns
91        self.deleteRows = deleteRows
92        self.selectLockedCells = selectLockedCells
93        self.selectUnlockedCells = selectUnlockedCells
94        self.sort = sort
95        self.autoFilter = autoFilter
96        self.pivotTables = pivotTables
97        if password is not None:
98            self.password = password
99        self.algorithmName = algorithmName
100        self.saltValue = saltValue
101        self.spinCount = spinCount
102        self.hashValue = hashValue
103
104
105    def set_password(self, value='', already_hashed=False):
106        super().set_password(value, already_hashed)
107        self.enable()
108
109
110    def enable(self):
111        self.sheet = True
112
113
114    def disable(self):
115        self.sheet = False
116
117
118    def  __bool__(self):
119        return self.sheet
120