workbook.py 6.9 KB

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