Source code

Download source archive ↓

src/openpyxl/packaging/workbook.py

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