workbook.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # Copyright (c) 2010-2024 openpyxl
  2. from openpyxl.descriptors.serialisable import Serialisable
  3. from openpyxl.descriptors import (
  4. Alias,
  5. Typed,
  6. String,
  7. Integer,
  8. Bool,
  9. NoneSet,
  10. )
  11. from openpyxl.descriptors.excel import ExtensionList, Relation
  12. from openpyxl.descriptors.sequence import NestedSequence
  13. from openpyxl.descriptors.nested import NestedString
  14. from openpyxl.xml.constants import SHEET_MAIN_NS
  15. from openpyxl.workbook.defined_name import DefinedNameList
  16. from openpyxl.workbook.external_reference import ExternalReference
  17. from openpyxl.workbook.function_group import FunctionGroupList
  18. from openpyxl.workbook.properties import WorkbookProperties, CalcProperties, FileVersion
  19. from openpyxl.workbook.protection import WorkbookProtection, FileSharing
  20. from openpyxl.workbook.smart_tags import SmartTagList, SmartTagProperties
  21. from openpyxl.workbook.views import CustomWorkbookView, BookView
  22. from openpyxl.workbook.web import WebPublishing, WebPublishObjectList
  23. class FileRecoveryProperties(Serialisable):
  24. tagname = "fileRecoveryPr"
  25. autoRecover = Bool(allow_none=True)
  26. crashSave = Bool(allow_none=True)
  27. dataExtractLoad = Bool(allow_none=True)
  28. repairLoad = Bool(allow_none=True)
  29. def __init__(self,
  30. autoRecover=None,
  31. crashSave=None,
  32. dataExtractLoad=None,
  33. repairLoad=None,
  34. ):
  35. self.autoRecover = autoRecover
  36. self.crashSave = crashSave
  37. self.dataExtractLoad = dataExtractLoad
  38. self.repairLoad = repairLoad
  39. class ChildSheet(Serialisable):
  40. """
  41. Represents a reference to a worksheet or chartsheet in workbook.xml
  42. It contains the title, order and state but only an indirect reference to
  43. the objects themselves.
  44. """
  45. tagname = "sheet"
  46. name = String()
  47. sheetId = Integer()
  48. state = NoneSet(values=(['visible', 'hidden', 'veryHidden']))
  49. id = Relation()
  50. def __init__(self,
  51. name=None,
  52. sheetId=None,
  53. state="visible",
  54. id=None,
  55. ):
  56. self.name = name
  57. self.sheetId = sheetId
  58. self.state = state
  59. self.id = id
  60. class PivotCache(Serialisable):
  61. tagname = "pivotCache"
  62. cacheId = Integer()
  63. id = Relation()
  64. def __init__(self,
  65. cacheId=None,
  66. id=None
  67. ):
  68. self.cacheId = cacheId
  69. self.id = id
  70. class WorkbookPackage(Serialisable):
  71. """
  72. Represent the workbook file in the archive
  73. """
  74. tagname = "workbook"
  75. conformance = NoneSet(values=['strict', 'transitional'])
  76. fileVersion = Typed(expected_type=FileVersion, allow_none=True)
  77. fileSharing = Typed(expected_type=FileSharing, allow_none=True)
  78. workbookPr = Typed(expected_type=WorkbookProperties, allow_none=True)
  79. properties = Alias("workbookPr")
  80. workbookProtection = Typed(expected_type=WorkbookProtection, allow_none=True)
  81. bookViews = NestedSequence(expected_type=BookView)
  82. sheets = NestedSequence(expected_type=ChildSheet)
  83. functionGroups = Typed(expected_type=FunctionGroupList, allow_none=True)
  84. externalReferences = NestedSequence(expected_type=ExternalReference)
  85. definedNames = Typed(expected_type=DefinedNameList, allow_none=True)
  86. calcPr = Typed(expected_type=CalcProperties, allow_none=True)
  87. oleSize = NestedString(allow_none=True, attribute="ref")
  88. customWorkbookViews = NestedSequence(expected_type=CustomWorkbookView)
  89. pivotCaches = NestedSequence(expected_type=PivotCache, allow_none=True)
  90. smartTagPr = Typed(expected_type=SmartTagProperties, allow_none=True)
  91. smartTagTypes = Typed(expected_type=SmartTagList, allow_none=True)
  92. webPublishing = Typed(expected_type=WebPublishing, allow_none=True)
  93. fileRecoveryPr = Typed(expected_type=FileRecoveryProperties, allow_none=True)
  94. webPublishObjects = Typed(expected_type=WebPublishObjectList, allow_none=True)
  95. extLst = Typed(expected_type=ExtensionList, allow_none=True)
  96. Ignorable = NestedString(namespace="http://schemas.openxmlformats.org/markup-compatibility/2006", allow_none=True)
  97. __elements__ = ('fileVersion', 'fileSharing', 'workbookPr',
  98. 'workbookProtection', 'bookViews', 'sheets', 'functionGroups',
  99. 'externalReferences', 'definedNames', 'calcPr', 'oleSize',
  100. 'customWorkbookViews', 'pivotCaches', 'smartTagPr', 'smartTagTypes',
  101. 'webPublishing', 'fileRecoveryPr', 'webPublishObjects')
  102. def __init__(self,
  103. conformance=None,
  104. fileVersion=None,
  105. fileSharing=None,
  106. workbookPr=None,
  107. workbookProtection=None,
  108. bookViews=(),
  109. sheets=(),
  110. functionGroups=None,
  111. externalReferences=(),
  112. definedNames=None,
  113. calcPr=None,
  114. oleSize=None,
  115. customWorkbookViews=(),
  116. pivotCaches=(),
  117. smartTagPr=None,
  118. smartTagTypes=None,
  119. webPublishing=None,
  120. fileRecoveryPr=None,
  121. webPublishObjects=None,
  122. extLst=None,
  123. Ignorable=None,
  124. ):
  125. self.conformance = conformance
  126. self.fileVersion = fileVersion
  127. self.fileSharing = fileSharing
  128. if workbookPr is None:
  129. workbookPr = WorkbookProperties()
  130. self.workbookPr = workbookPr
  131. self.workbookProtection = workbookProtection
  132. self.bookViews = bookViews
  133. self.sheets = sheets
  134. self.functionGroups = functionGroups
  135. self.externalReferences = externalReferences
  136. self.definedNames = definedNames
  137. self.calcPr = calcPr
  138. self.oleSize = oleSize
  139. self.customWorkbookViews = customWorkbookViews
  140. self.pivotCaches = pivotCaches
  141. self.smartTagPr = smartTagPr
  142. self.smartTagTypes = smartTagTypes
  143. self.webPublishing = webPublishing
  144. self.fileRecoveryPr = fileRecoveryPr
  145. self.webPublishObjects = webPublishObjects
  146. def to_tree(self):
  147. tree = super().to_tree()
  148. tree.set("xmlns", SHEET_MAIN_NS)
  149. return tree
  150. @property
  151. def active(self):
  152. for view in self.bookViews:
  153. if view.activeTab is not None:
  154. return view.activeTab
  155. return 0