protection.py 3.8 KB

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