1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
|
# Copyright (c) 2010-2024 openpyxl
from collections import defaultdict
from openpyxl.descriptors.serialisable import Serialisable
from openpyxl.descriptors import (
Typed,
Integer,
NoneSet,
Set,
Bool,
String,
Bool,
Sequence,
)
from openpyxl.descriptors.excel import ExtensionList, Relation
from openpyxl.descriptors.sequence import NestedSequence
from openpyxl.xml.constants import SHEET_MAIN_NS
from openpyxl.xml.functions import tostring
from openpyxl.packaging.relationship import (
RelationshipList,
Relationship,
get_rels_path
)
from .fields import Index
from openpyxl.worksheet.filters import (
AutoFilter,
)
class HierarchyUsage(Serialisable):
tagname = "hierarchyUsage"
hierarchyUsage = Integer()
def __init__(self,
hierarchyUsage=None,
):
self.hierarchyUsage = hierarchyUsage
class ColHierarchiesUsage(Serialisable):
tagname = "colHierarchiesUsage"
colHierarchyUsage = Sequence(expected_type=HierarchyUsage, )
__elements__ = ('colHierarchyUsage',)
__attrs__ = ('count', )
def __init__(self,
count=None,
colHierarchyUsage=(),
):
self.colHierarchyUsage = colHierarchyUsage
@property
def count(self):
return len(self.colHierarchyUsage)
class RowHierarchiesUsage(Serialisable):
tagname = "rowHierarchiesUsage"
rowHierarchyUsage = Sequence(expected_type=HierarchyUsage, )
__elements__ = ('rowHierarchyUsage',)
__attrs__ = ('count', )
def __init__(self,
count=None,
rowHierarchyUsage=(),
):
self.rowHierarchyUsage = rowHierarchyUsage
@property
def count(self):
return len(self.rowHierarchyUsage)
class PivotFilter(Serialisable):
tagname = "filter"
fld = Integer()
mpFld = Integer(allow_none=True)
type = Set(values=(['unknown', 'count', 'percent', 'sum', 'captionEqual',
'captionNotEqual', 'captionBeginsWith', 'captionNotBeginsWith',
'captionEndsWith', 'captionNotEndsWith', 'captionContains',
'captionNotContains', 'captionGreaterThan', 'captionGreaterThanOrEqual',
'captionLessThan', 'captionLessThanOrEqual', 'captionBetween',
'captionNotBetween', 'valueEqual', 'valueNotEqual', 'valueGreaterThan',
'valueGreaterThanOrEqual', 'valueLessThan', 'valueLessThanOrEqual',
'valueBetween', 'valueNotBetween', 'dateEqual', 'dateNotEqual',
'dateOlderThan', 'dateOlderThanOrEqual', 'dateNewerThan',
'dateNewerThanOrEqual', 'dateBetween', 'dateNotBetween', 'tomorrow',
'today', 'yesterday', 'nextWeek', 'thisWeek', 'lastWeek', 'nextMonth',
'thisMonth', 'lastMonth', 'nextQuarter', 'thisQuarter', 'lastQuarter',
'nextYear', 'thisYear', 'lastYear', 'yearToDate', 'Q1', 'Q2', 'Q3', 'Q4',
'M1', 'M2', 'M3', 'M4', 'M5', 'M6', 'M7', 'M8', 'M9', 'M10', 'M11',
'M12']))
evalOrder = Integer(allow_none=True)
id = Integer()
iMeasureHier = Integer(allow_none=True)
iMeasureFld = Integer(allow_none=True)
name = String(allow_none=True)
description = String(allow_none=True)
stringValue1 = String(allow_none=True)
stringValue2 = String(allow_none=True)
autoFilter = Typed(expected_type=AutoFilter, )
extLst = Typed(expected_type=ExtensionList, allow_none=True)
__elements__ = ('autoFilter',)
def __init__(self,
fld=None,
mpFld=None,
type=None,
evalOrder=None,
id=None,
iMeasureHier=None,
iMeasureFld=None,
name=None,
description=None,
stringValue1=None,
stringValue2=None,
autoFilter=None,
extLst=None,
):
self.fld = fld
self.mpFld = mpFld
self.type = type
self.evalOrder = evalOrder
self.id = id
self.iMeasureHier = iMeasureHier
self.iMeasureFld = iMeasureFld
self.name = name
self.description = description
self.stringValue1 = stringValue1
self.stringValue2 = stringValue2
self.autoFilter = autoFilter
class PivotFilters(Serialisable):
count = Integer()
filter = Typed(expected_type=PivotFilter, allow_none=True)
__elements__ = ('filter',)
def __init__(self,
count=None,
filter=None,
):
self.filter = filter
class PivotTableStyle(Serialisable):
tagname = "pivotTableStyleInfo"
name = String(allow_none=True)
showRowHeaders = Bool()
showColHeaders = Bool()
showRowStripes = Bool()
showColStripes = Bool()
showLastColumn = Bool()
def __init__(self,
name=None,
showRowHeaders=None,
showColHeaders=None,
showRowStripes=None,
showColStripes=None,
showLastColumn=None,
):
self.name = name
self.showRowHeaders = showRowHeaders
self.showColHeaders = showColHeaders
self.showRowStripes = showRowStripes
self.showColStripes = showColStripes
self.showLastColumn = showLastColumn
class MemberList(Serialisable):
tagname = "members"
level = Integer(allow_none=True)
member = NestedSequence(expected_type=String, attribute="name")
__elements__ = ('member',)
def __init__(self,
count=None,
level=None,
member=(),
):
self.level = level
self.member = member
@property
def count(self):
return len(self.member)
class MemberProperty(Serialisable):
tagname = "mps"
name = String(allow_none=True)
showCell = Bool(allow_none=True)
showTip = Bool(allow_none=True)
showAsCaption = Bool(allow_none=True)
nameLen = Integer(allow_none=True)
pPos = Integer(allow_none=True)
pLen = Integer(allow_none=True)
level = Integer(allow_none=True)
field = Integer()
def __init__(self,
name=None,
showCell=None,
showTip=None,
showAsCaption=None,
nameLen=None,
pPos=None,
pLen=None,
level=None,
field=None,
):
self.name = name
self.showCell = showCell
self.showTip = showTip
self.showAsCaption = showAsCaption
self.nameLen = nameLen
self.pPos = pPos
self.pLen = pLen
self.level = level
self.field = field
class PivotHierarchy(Serialisable):
tagname = "pivotHierarchy"
outline = Bool()
multipleItemSelectionAllowed = Bool()
subtotalTop = Bool()
showInFieldList = Bool()
dragToRow = Bool()
dragToCol = Bool()
dragToPage = Bool()
dragToData = Bool()
dragOff = Bool()
includeNewItemsInFilter = Bool()
caption = String(allow_none=True)
mps = NestedSequence(expected_type=MemberProperty, count=True)
members = Typed(expected_type=MemberList, allow_none=True)
extLst = Typed(expected_type=ExtensionList, allow_none=True)
__elements__ = ('mps', 'members',)
def __init__(self,
outline=None,
multipleItemSelectionAllowed=None,
subtotalTop=None,
showInFieldList=None,
dragToRow=None,
dragToCol=None,
dragToPage=None,
dragToData=None,
dragOff=None,
includeNewItemsInFilter=None,
caption=None,
mps=(),
members=None,
extLst=None,
):
self.outline = outline
self.multipleItemSelectionAllowed = multipleItemSelectionAllowed
self.subtotalTop = subtotalTop
self.showInFieldList = showInFieldList
self.dragToRow = dragToRow
self.dragToCol = dragToCol
self.dragToPage = dragToPage
self.dragToData = dragToData
self.dragOff = dragOff
self.includeNewItemsInFilter = includeNewItemsInFilter
self.caption = caption
self.mps = mps
self.members = members
self.extLst = extLst
class Reference(Serialisable):
tagname = "reference"
field = Integer(allow_none=True)
selected = Bool(allow_none=True)
byPosition = Bool(allow_none=True)
relative = Bool(allow_none=True)
defaultSubtotal = Bool(allow_none=True)
sumSubtotal = Bool(allow_none=True)
countASubtotal = Bool(allow_none=True)
avgSubtotal = Bool(allow_none=True)
maxSubtotal = Bool(allow_none=True)
minSubtotal = Bool(allow_none=True)
productSubtotal = Bool(allow_none=True)
countSubtotal = Bool(allow_none=True)
stdDevSubtotal = Bool(allow_none=True)
stdDevPSubtotal = Bool(allow_none=True)
varSubtotal = Bool(allow_none=True)
varPSubtotal = Bool(allow_none=True)
x = Sequence(expected_type=Index)
extLst = Typed(expected_type=ExtensionList, allow_none=True)
__elements__ = ('x',)
def __init__(self,
field=None,
count=None,
selected=None,
byPosition=None,
relative=None,
defaultSubtotal=None,
sumSubtotal=None,
countASubtotal=None,
avgSubtotal=None,
maxSubtotal=None,
minSubtotal=None,
productSubtotal=None,
countSubtotal=None,
stdDevSubtotal=None,
stdDevPSubtotal=None,
varSubtotal=None,
varPSubtotal=None,
x=(),
extLst=None,
):
self.field = field
self.selected = selected
self.byPosition = byPosition
self.relative = relative
self.defaultSubtotal = defaultSubtotal
self.sumSubtotal = sumSubtotal
self.countASubtotal = countASubtotal
self.avgSubtotal = avgSubtotal
self.maxSubtotal = maxSubtotal
self.minSubtotal = minSubtotal
self.productSubtotal = productSubtotal
self.countSubtotal = countSubtotal
self.stdDevSubtotal = stdDevSubtotal
self.stdDevPSubtotal = stdDevPSubtotal
self.varSubtotal = varSubtotal
self.varPSubtotal = varPSubtotal
self.x = x
@property
def count(self):
return len(self.field)
class PivotArea(Serialisable):
tagname = "pivotArea"
references = NestedSequence(expected_type=Reference, count=True)
extLst = Typed(expected_type=ExtensionList, allow_none=True)
field = Integer(allow_none=True)
type = NoneSet(values=(['normal', 'data', 'all', 'origin', 'button',
'topEnd', 'topRight']))
dataOnly = Bool(allow_none=True)
labelOnly = Bool(allow_none=True)
grandRow = Bool(allow_none=True)
grandCol = Bool(allow_none=True)
cacheIndex = Bool(allow_none=True)
outline = Bool(allow_none=True)
offset = String(allow_none=True)
collapsedLevelsAreSubtotals = Bool(allow_none=True)
axis = NoneSet(values=(['axisRow', 'axisCol', 'axisPage', 'axisValues']))
fieldPosition = Integer(allow_none=True)
__elements__ = ('references',)
def __init__(self,
references=(),
extLst=None,
field=None,
type="normal",
dataOnly=True,
labelOnly=None,
grandRow=None,
grandCol=None,
cacheIndex=None,
outline=True,
offset=None,
collapsedLevelsAreSubtotals=None,
axis=None,
fieldPosition=None,
):
self.references = references
self.extLst = extLst
self.field = field
self.type = type
self.dataOnly = dataOnly
self.labelOnly = labelOnly
self.grandRow = grandRow
self.grandCol = grandCol
self.cacheIndex = cacheIndex
self.outline = outline
self.offset = offset
self.collapsedLevelsAreSubtotals = collapsedLevelsAreSubtotals
self.axis = axis
self.fieldPosition = fieldPosition
class ChartFormat(Serialisable):
tagname = "chartFormat"
chart = Integer()
format = Integer()
series = Bool()
pivotArea = Typed(expected_type=PivotArea, )
__elements__ = ('pivotArea',)
def __init__(self,
chart=None,
format=None,
series=None,
pivotArea=None,
):
self.chart = chart
self.format = format
self.series = series
self.pivotArea = pivotArea
class ConditionalFormat(Serialisable):
tagname = "conditionalFormat"
scope = Set(values=(['selection', 'data', 'field']))
type = NoneSet(values=(['all', 'row', 'column']))
priority = Integer()
pivotAreas = NestedSequence(expected_type=PivotArea)
extLst = Typed(expected_type=ExtensionList, allow_none=True)
__elements__ = ('pivotAreas',)
def __init__(self,
scope="selection",
type=None,
priority=None,
pivotAreas=(),
extLst=None,
):
self.scope = scope
self.type = type
self.priority = priority
self.pivotAreas = pivotAreas
self.extLst = extLst
class ConditionalFormatList(Serialisable):
tagname = "conditionalFormats"
conditionalFormat = Sequence(expected_type=ConditionalFormat)
__attrs__ = ("count",)
def __init__(self, conditionalFormat=(), count=None):
self.conditionalFormat = conditionalFormat
def by_priority(self):
"""
Return a dictionary of format objects keyed by (field id and format property).
This can be used to map the formats to field but also to dedupe to match
worksheet definitions which are grouped by cell range
"""
fmts = {}
for fmt in self.conditionalFormat:
for area in fmt.pivotAreas:
for ref in area.references:
for field in ref.x:
key = (field.v, fmt.priority)
fmts[key] = fmt
return fmts
def _dedupe(self):
"""
Group formats by field index and priority.
Sorted to match sorting and grouping for corresponding worksheet formats
The implemtenters notes contain significant deviance from the OOXML
specification, in particular how conditional formats in tables relate to
those defined in corresponding worksheets and how to determine which
format applies to which fields.
There are some magical interdependencies:
* Every pivot table fmt must have a worksheet cxf with the same priority.
* In the reference part the field 4294967294 refers to a data field, the
spec says -2
* Data fields are referenced by the 0-index reference.x.v value
Things are made more complicated by the fact that field items behave
diffently if the parent is a reference or shared item: "In Office if the
parent is the reference element, then restrictions of this value are
defined by reference@field. If the parent is the tables element, then
this value specifies the index into the table tag position in @url."
Yeah, right!
"""
fmts = self.by_priority()
# sort by priority in order, keeping the highest numerical priority, least when
# actually applied
# this is not documented but it's what Excel is happy with
fmts = {field:fmt for (field, priority), fmt in sorted(fmts.items(), reverse=True)}
#fmts = {field:fmt for (field, priority), fmt in fmts.items()}
if fmts:
self.conditionalFormat = list(fmts.values())
@property
def count(self):
return len(self.conditionalFormat)
def to_tree(self, tagname=None):
self._dedupe()
return super().to_tree(tagname)
class Format(Serialisable):
tagname = "format"
action = NoneSet(values=(['blank', 'formatting', 'drill', 'formula']))
dxfId = Integer(allow_none=True)
pivotArea = Typed(expected_type=PivotArea, )
extLst = Typed(expected_type=ExtensionList, allow_none=True)
__elements__ = ('pivotArea',)
def __init__(self,
action="formatting",
dxfId=None,
pivotArea=None,
extLst=None,
):
self.action = action
self.dxfId = dxfId
self.pivotArea = pivotArea
self.extLst = extLst
class DataField(Serialisable):
tagname = "dataField"
name = String(allow_none=True)
fld = Integer()
subtotal = Set(values=(['average', 'count', 'countNums', 'max', 'min',
'product', 'stdDev', 'stdDevp', 'sum', 'var', 'varp']))
showDataAs = Set(values=(['normal', 'difference', 'percent',
'percentDiff', 'runTotal', 'percentOfRow', 'percentOfCol',
'percentOfTotal', 'index']))
baseField = Integer()
baseItem = Integer()
numFmtId = Integer(allow_none=True)
extLst = Typed(expected_type=ExtensionList, allow_none=True)
__elements__ = ()
def __init__(self,
name=None,
fld=None,
subtotal="sum",
showDataAs="normal",
baseField=-1,
baseItem=1048832,
numFmtId=None,
extLst=None,
):
self.name = name
self.fld = fld
self.subtotal = subtotal
self.showDataAs = showDataAs
self.baseField = baseField
self.baseItem = baseItem
self.numFmtId = numFmtId
self.extLst = extLst
class PageField(Serialisable):
tagname = "pageField"
fld = Integer()
item = Integer(allow_none=True)
hier = Integer(allow_none=True)
name = String(allow_none=True)
cap = String(allow_none=True)
extLst = Typed(expected_type=ExtensionList, allow_none=True)
__elements__ = ()
def __init__(self,
fld=None,
item=None,
hier=None,
name=None,
cap=None,
extLst=None,
):
self.fld = fld
self.item = item
self.hier = hier
self.name = name
self.cap = cap
self.extLst = extLst
class RowColItem(Serialisable):
tagname = "i"
t = Set(values=(['data', 'default', 'sum', 'countA', 'avg', 'max', 'min',
'product', 'count', 'stdDev', 'stdDevP', 'var', 'varP', 'grand',
'blank']))
r = Integer()
i = Integer()
x = Sequence(expected_type=Index, attribute="v")
__elements__ = ('x',)
def __init__(self,
t="data",
r=0,
i=0,
x=(),
):
self.t = t
self.r = r
self.i = i
self.x = x
class RowColField(Serialisable):
tagname = "field"
x = Integer()
def __init__(self,
x=None,
):
self.x = x
class AutoSortScope(Serialisable):
pivotArea = Typed(expected_type=PivotArea, )
__elements__ = ('pivotArea',)
def __init__(self,
pivotArea=None,
):
self.pivotArea = pivotArea
class FieldItem(Serialisable):
tagname = "item"
n = String(allow_none=True)
t = Set(values=(['data', 'default', 'sum', 'countA', 'avg', 'max', 'min',
'product', 'count', 'stdDev', 'stdDevP', 'var', 'varP', 'grand',
'blank']))
h = Bool(allow_none=True)
s = Bool(allow_none=True)
sd = Bool(allow_none=True)
f = Bool(allow_none=True)
m = Bool(allow_none=True)
c = Bool(allow_none=True)
x = Integer(allow_none=True)
d = Bool(allow_none=True)
e = Bool(allow_none=True)
def __init__(self,
n=None,
t="data",
h=None,
s=None,
sd=True,
f=None,
m=None,
c=None,
x=None,
d=None,
e=None,
):
self.n = n
self.t = t
self.h = h
self.s = s
self.sd = sd
self.f = f
self.m = m
self.c = c
self.x = x
self.d = d
self.e = e
class PivotField(Serialisable):
tagname = "pivotField"
items = NestedSequence(expected_type=FieldItem, count=True)
autoSortScope = Typed(expected_type=AutoSortScope, allow_none=True)
extLst = Typed(expected_type=ExtensionList, allow_none=True)
name = String(allow_none=True)
axis = NoneSet(values=(['axisRow', 'axisCol', 'axisPage', 'axisValues']))
dataField = Bool(allow_none=True)
subtotalCaption = String(allow_none=True)
showDropDowns = Bool(allow_none=True)
hiddenLevel = Bool(allow_none=True)
uniqueMemberProperty = String(allow_none=True)
compact = Bool(allow_none=True)
allDrilled = Bool(allow_none=True)
numFmtId = Integer(allow_none=True)
outline = Bool(allow_none=True)
subtotalTop = Bool(allow_none=True)
dragToRow = Bool(allow_none=True)
dragToCol = Bool(allow_none=True)
multipleItemSelectionAllowed = Bool(allow_none=True)
dragToPage = Bool(allow_none=True)
dragToData = Bool(allow_none=True)
dragOff = Bool(allow_none=True)
showAll = Bool(allow_none=True)
insertBlankRow = Bool(allow_none=True)
serverField = Bool(allow_none=True)
insertPageBreak = Bool(allow_none=True)
autoShow = Bool(allow_none=True)
topAutoShow = Bool(allow_none=True)
hideNewItems = Bool(allow_none=True)
measureFilter = Bool(allow_none=True)
includeNewItemsInFilter = Bool(allow_none=True)
itemPageCount = Integer(allow_none=True)
sortType = Set(values=(['manual', 'ascending', 'descending']))
dataSourceSort = Bool(allow_none=True)
nonAutoSortDefault = Bool(allow_none=True)
rankBy = Integer(allow_none=True)
defaultSubtotal = Bool(allow_none=True)
sumSubtotal = Bool(allow_none=True)
countASubtotal = Bool(allow_none=True)
avgSubtotal = Bool(allow_none=True)
maxSubtotal = Bool(allow_none=True)
minSubtotal = Bool(allow_none=True)
productSubtotal = Bool(allow_none=True)
countSubtotal = Bool(allow_none=True)
stdDevSubtotal = Bool(allow_none=True)
stdDevPSubtotal = Bool(allow_none=True)
varSubtotal = Bool(allow_none=True)
varPSubtotal = Bool(allow_none=True)
showPropCell = Bool(allow_none=True)
showPropTip = Bool(allow_none=True)
showPropAsCaption = Bool(allow_none=True)
defaultAttributeDrillState = Bool(allow_none=True)
__elements__ = ('items', 'autoSortScope',)
def __init__(self,
items=(),
autoSortScope=None,
name=None,
axis=None,
dataField=None,
subtotalCaption=None,
showDropDowns=True,
hiddenLevel=None,
uniqueMemberProperty=None,
compact=True,
allDrilled=None,
numFmtId=None,
outline=True,
subtotalTop=True,
dragToRow=True,
dragToCol=True,
multipleItemSelectionAllowed=None,
dragToPage=True,
dragToData=True,
dragOff=True,
showAll=True,
insertBlankRow=None,
serverField=None,
insertPageBreak=None,
autoShow=None,
topAutoShow=True,
hideNewItems=None,
measureFilter=None,
includeNewItemsInFilter=None,
itemPageCount=10,
sortType="manual",
dataSourceSort=None,
nonAutoSortDefault=None,
rankBy=None,
defaultSubtotal=True,
sumSubtotal=None,
countASubtotal=None,
avgSubtotal=None,
maxSubtotal=None,
minSubtotal=None,
productSubtotal=None,
countSubtotal=None,
stdDevSubtotal=None,
stdDevPSubtotal=None,
varSubtotal=None,
varPSubtotal=None,
showPropCell=None,
showPropTip=None,
showPropAsCaption=None,
defaultAttributeDrillState=None,
extLst=None,
):
self.items = items
self.autoSortScope = autoSortScope
self.name = name
self.axis = axis
self.dataField = dataField
self.subtotalCaption = subtotalCaption
self.showDropDowns = showDropDowns
self.hiddenLevel = hiddenLevel
self.uniqueMemberProperty = uniqueMemberProperty
self.compact = compact
self.allDrilled = allDrilled
self.numFmtId = numFmtId
self.outline = outline
self.subtotalTop = subtotalTop
self.dragToRow = dragToRow
self.dragToCol = dragToCol
self.multipleItemSelectionAllowed = multipleItemSelectionAllowed
self.dragToPage = dragToPage
self.dragToData = dragToData
self.dragOff = dragOff
self.showAll = showAll
self.insertBlankRow = insertBlankRow
self.serverField = serverField
self.insertPageBreak = insertPageBreak
self.autoShow = autoShow
self.topAutoShow = topAutoShow
self.hideNewItems = hideNewItems
self.measureFilter = measureFilter
self.includeNewItemsInFilter = includeNewItemsInFilter
self.itemPageCount = itemPageCount
self.sortType = sortType
self.dataSourceSort = dataSourceSort
self.nonAutoSortDefault = nonAutoSortDefault
self.rankBy = rankBy
self.defaultSubtotal = defaultSubtotal
self.sumSubtotal = sumSubtotal
self.countASubtotal = countASubtotal
self.avgSubtotal = avgSubtotal
self.maxSubtotal = maxSubtotal
self.minSubtotal = minSubtotal
self.productSubtotal = productSubtotal
self.countSubtotal = countSubtotal
self.stdDevSubtotal = stdDevSubtotal
self.stdDevPSubtotal = stdDevPSubtotal
self.varSubtotal = varSubtotal
self.varPSubtotal = varPSubtotal
self.showPropCell = showPropCell
self.showPropTip = showPropTip
self.showPropAsCaption = showPropAsCaption
self.defaultAttributeDrillState = defaultAttributeDrillState
class Location(Serialisable):
tagname = "location"
ref = String()
firstHeaderRow = Integer()
firstDataRow = Integer()
firstDataCol = Integer()
rowPageCount = Integer(allow_none=True)
colPageCount = Integer(allow_none=True)
def __init__(self,
ref=None,
firstHeaderRow=None,
firstDataRow=None,
firstDataCol=None,
rowPageCount=None,
colPageCount=None,
):
self.ref = ref
self.firstHeaderRow = firstHeaderRow
self.firstDataRow = firstDataRow
self.firstDataCol = firstDataCol
self.rowPageCount = rowPageCount
self.colPageCount = colPageCount
class TableDefinition(Serialisable):
mime_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotTable+xml"
rel_type = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotTable"
_id = 1
_path = "/xl/pivotTables/pivotTable{0}.xml"
tagname = "pivotTableDefinition"
cache = None
name = String()
cacheId = Integer()
dataOnRows = Bool()
dataPosition = Integer(allow_none=True)
dataCaption = String()
grandTotalCaption = String(allow_none=True)
errorCaption = String(allow_none=True)
showError = Bool()
missingCaption = String(allow_none=True)
showMissing = Bool()
pageStyle = String(allow_none=True)
pivotTableStyle = String(allow_none=True)
vacatedStyle = String(allow_none=True)
tag = String(allow_none=True)
updatedVersion = Integer()
minRefreshableVersion = Integer()
asteriskTotals = Bool()
showItems = Bool()
editData = Bool()
disableFieldList = Bool()
showCalcMbrs = Bool()
visualTotals = Bool()
showMultipleLabel = Bool()
showDataDropDown = Bool()
showDrill = Bool()
printDrill = Bool()
showMemberPropertyTips = Bool()
showDataTips = Bool()
enableWizard = Bool()
enableDrill = Bool()
enableFieldProperties = Bool()
preserveFormatting = Bool()
useAutoFormatting = Bool()
pageWrap = Integer()
pageOverThenDown = Bool()
subtotalHiddenItems = Bool()
rowGrandTotals = Bool()
colGrandTotals = Bool()
fieldPrintTitles = Bool()
itemPrintTitles = Bool()
mergeItem = Bool()
showDropZones = Bool()
createdVersion = Integer()
indent = Integer()
showEmptyRow = Bool()
showEmptyCol = Bool()
showHeaders = Bool()
compact = Bool()
outline = Bool()
outlineData = Bool()
compactData = Bool()
published = Bool()
gridDropZones = Bool()
immersive = Bool()
multipleFieldFilters = Bool()
chartFormat = Integer()
rowHeaderCaption = String(allow_none=True)
colHeaderCaption = String(allow_none=True)
fieldListSortAscending = Bool()
mdxSubqueries = Bool()
customListSort = Bool(allow_none=True)
autoFormatId = Integer(allow_none=True)
applyNumberFormats = Bool()
applyBorderFormats = Bool()
applyFontFormats = Bool()
applyPatternFormats = Bool()
applyAlignmentFormats = Bool()
applyWidthHeightFormats = Bool()
location = Typed(expected_type=Location, )
pivotFields = NestedSequence(expected_type=PivotField, count=True)
rowFields = NestedSequence(expected_type=RowColField, count=True)
rowItems = NestedSequence(expected_type=RowColItem, count=True)
colFields = NestedSequence(expected_type=RowColField, count=True)
colItems = NestedSequence(expected_type=RowColItem, count=True)
pageFields = NestedSequence(expected_type=PageField, count=True)
dataFields = NestedSequence(expected_type=DataField, count=True)
formats = NestedSequence(expected_type=Format, count=True)
conditionalFormats = Typed(expected_type=ConditionalFormatList, allow_none=True)
chartFormats = NestedSequence(expected_type=ChartFormat, count=True)
pivotHierarchies = NestedSequence(expected_type=PivotHierarchy, count=True)
pivotTableStyleInfo = Typed(expected_type=PivotTableStyle, allow_none=True)
filters = NestedSequence(expected_type=PivotFilter, count=True)
rowHierarchiesUsage = Typed(expected_type=RowHierarchiesUsage, allow_none=True)
colHierarchiesUsage = Typed(expected_type=ColHierarchiesUsage, allow_none=True)
extLst = Typed(expected_type=ExtensionList, allow_none=True)
id = Relation()
__elements__ = ('location', 'pivotFields', 'rowFields', 'rowItems',
'colFields', 'colItems', 'pageFields', 'dataFields', 'formats',
'conditionalFormats', 'chartFormats', 'pivotHierarchies',
'pivotTableStyleInfo', 'filters', 'rowHierarchiesUsage',
'colHierarchiesUsage',)
def __init__(self,
name=None,
cacheId=None,
dataOnRows=False,
dataPosition=None,
dataCaption=None,
grandTotalCaption=None,
errorCaption=None,
showError=False,
missingCaption=None,
showMissing=True,
pageStyle=None,
pivotTableStyle=None,
vacatedStyle=None,
tag=None,
updatedVersion=0,
minRefreshableVersion=0,
asteriskTotals=False,
showItems=True,
editData=False,
disableFieldList=False,
showCalcMbrs=True,
visualTotals=True,
showMultipleLabel=True,
showDataDropDown=True,
showDrill=True,
printDrill=False,
showMemberPropertyTips=True,
showDataTips=True,
enableWizard=True,
enableDrill=True,
enableFieldProperties=True,
preserveFormatting=True,
useAutoFormatting=False,
pageWrap=0,
pageOverThenDown=False,
subtotalHiddenItems=False,
rowGrandTotals=True,
colGrandTotals=True,
fieldPrintTitles=False,
itemPrintTitles=False,
mergeItem=False,
showDropZones=True,
createdVersion=0,
indent=1,
showEmptyRow=False,
showEmptyCol=False,
showHeaders=True,
compact=True,
outline=False,
outlineData=False,
compactData=True,
published=False,
gridDropZones=False,
immersive=True,
multipleFieldFilters=None,
chartFormat=0,
rowHeaderCaption=None,
colHeaderCaption=None,
fieldListSortAscending=None,
mdxSubqueries=None,
customListSort=None,
autoFormatId=None,
applyNumberFormats=False,
applyBorderFormats=False,
applyFontFormats=False,
applyPatternFormats=False,
applyAlignmentFormats=False,
applyWidthHeightFormats=False,
location=None,
pivotFields=(),
rowFields=(),
rowItems=(),
colFields=(),
colItems=(),
pageFields=(),
dataFields=(),
formats=(),
conditionalFormats=None,
chartFormats=(),
pivotHierarchies=(),
pivotTableStyleInfo=None,
filters=(),
rowHierarchiesUsage=None,
colHierarchiesUsage=None,
extLst=None,
id=None,
):
self.name = name
self.cacheId = cacheId
self.dataOnRows = dataOnRows
self.dataPosition = dataPosition
self.dataCaption = dataCaption
self.grandTotalCaption = grandTotalCaption
self.errorCaption = errorCaption
self.showError = showError
self.missingCaption = missingCaption
self.showMissing = showMissing
self.pageStyle = pageStyle
self.pivotTableStyle = pivotTableStyle
self.vacatedStyle = vacatedStyle
self.tag = tag
self.updatedVersion = updatedVersion
self.minRefreshableVersion = minRefreshableVersion
self.asteriskTotals = asteriskTotals
self.showItems = showItems
self.editData = editData
self.disableFieldList = disableFieldList
self.showCalcMbrs = showCalcMbrs
self.visualTotals = visualTotals
self.showMultipleLabel = showMultipleLabel
self.showDataDropDown = showDataDropDown
self.showDrill = showDrill
self.printDrill = printDrill
self.showMemberPropertyTips = showMemberPropertyTips
self.showDataTips = showDataTips
self.enableWizard = enableWizard
self.enableDrill = enableDrill
self.enableFieldProperties = enableFieldProperties
self.preserveFormatting = preserveFormatting
self.useAutoFormatting = useAutoFormatting
self.pageWrap = pageWrap
self.pageOverThenDown = pageOverThenDown
self.subtotalHiddenItems = subtotalHiddenItems
self.rowGrandTotals = rowGrandTotals
self.colGrandTotals = colGrandTotals
self.fieldPrintTitles = fieldPrintTitles
self.itemPrintTitles = itemPrintTitles
self.mergeItem = mergeItem
self.showDropZones = showDropZones
self.createdVersion = createdVersion
self.indent = indent
self.showEmptyRow = showEmptyRow
self.showEmptyCol = showEmptyCol
self.showHeaders = showHeaders
self.compact = compact
self.outline = outline
self.outlineData = outlineData
self.compactData = compactData
self.published = published
self.gridDropZones = gridDropZones
self.immersive = immersive
self.multipleFieldFilters = multipleFieldFilters
self.chartFormat = chartFormat
self.rowHeaderCaption = rowHeaderCaption
self.colHeaderCaption = colHeaderCaption
self.fieldListSortAscending = fieldListSortAscending
self.mdxSubqueries = mdxSubqueries
self.customListSort = customListSort
self.autoFormatId = autoFormatId
self.applyNumberFormats = applyNumberFormats
self.applyBorderFormats = applyBorderFormats
self.applyFontFormats = applyFontFormats
self.applyPatternFormats = applyPatternFormats
self.applyAlignmentFormats = applyAlignmentFormats
self.applyWidthHeightFormats = applyWidthHeightFormats
self.location = location
self.pivotFields = pivotFields
self.rowFields = rowFields
self.rowItems = rowItems
self.colFields = colFields
self.colItems = colItems
self.pageFields = pageFields
self.dataFields = dataFields
self.formats = formats
self.conditionalFormats = conditionalFormats
self.conditionalFormats = None
self.chartFormats = chartFormats
self.pivotHierarchies = pivotHierarchies
self.pivotTableStyleInfo = pivotTableStyleInfo
self.filters = filters
self.rowHierarchiesUsage = rowHierarchiesUsage
self.colHierarchiesUsage = colHierarchiesUsage
self.extLst = extLst
self.id = id
def to_tree(self):
tree = super().to_tree()
tree.set("xmlns", SHEET_MAIN_NS)
return tree
@property
def path(self):
return self._path.format(self._id)
def _write(self, archive, manifest):
"""
Add to zipfile and update manifest
"""
self._write_rels(archive, manifest)
xml = tostring(self.to_tree())
archive.writestr(self.path[1:], xml)
manifest.append(self)
def _write_rels(self, archive, manifest):
"""
Write the relevant child objects and add links
"""
if self.cache is None:
return
rels = RelationshipList()
r = Relationship(Type=self.cache.rel_type, Target=self.cache.path)
rels.append(r)
self.id = r.id
if self.cache.path[1:] not in archive.namelist():
self.cache._write(archive, manifest)
path = get_rels_path(self.path)
xml = tostring(rels.to_tree())
archive.writestr(path[1:], xml)
def formatted_fields(self):
"""Map fields to associated conditional formats by priority"""
if not self.conditionalFormats:
return {}
fields = defaultdict(list)
for idx, prio in self.conditionalFormats.by_priority():
name = self.dataFields[idx].name
fields[name].append(prio)
return fields
@property
def summary(self):
"""
Provide a simplified summary of the table
"""
return f"{self.name} {dict(self.location)}"
|