Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/openpyxl/worksheet/dimensions.py: 38%

161 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1# Copyright (c) 2010-2022 openpyxl 

2 

3from copy import copy 

4 

5from openpyxl.compat import safe_string 

6from openpyxl.utils import ( 

7 get_column_interval, 

8 column_index_from_string, 

9 range_boundaries, 

10) 

11from openpyxl.utils.units import DEFAULT_COLUMN_WIDTH 

12from openpyxl.descriptors import ( 

13 Integer, 

14 Float, 

15 Bool, 

16 Strict, 

17 String, 

18 Alias, 

19) 

20from openpyxl.descriptors.serialisable import Serialisable 

21from openpyxl.styles.styleable import StyleableObject 

22from openpyxl.utils.bound_dictionary import BoundDictionary 

23from openpyxl.xml.functions import Element 

24 

25 

26class Dimension(Strict, StyleableObject): 

27 """Information about the display properties of a row or column.""" 

28 __fields__ = ('hidden', 

29 'outlineLevel', 

30 'collapsed',) 

31 

32 index = Integer() 

33 hidden = Bool() 

34 outlineLevel = Integer(allow_none=True) 

35 outline_level = Alias('outlineLevel') 

36 collapsed = Bool() 

37 style = Alias('style_id') 

38 

39 

40 def __init__(self, index, hidden, outlineLevel, 

41 collapsed, worksheet, visible=True, style=None): 

42 super(Dimension, self).__init__(sheet=worksheet, style_array=style) 

43 self.index = index 

44 self.hidden = hidden 

45 self.outlineLevel = outlineLevel 

46 self.collapsed = collapsed 

47 

48 

49 def __iter__(self): 

50 for key in self.__fields__: 

51 value = getattr(self, key, None) 

52 if value: 

53 yield key, safe_string(value) 

54 

55 

56 def __copy__(self): 

57 cp = self.__new__(self.__class__) 

58 attrib = self.__dict__ 

59 attrib['worksheet'] = self.parent 

60 cp.__init__(**attrib) 

61 cp._style = copy(self._style) 

62 return cp 

63 

64 

65class RowDimension(Dimension): 

66 """Information about the display properties of a row.""" 

67 

68 __fields__ = Dimension.__fields__ + ('ht', 'customFormat', 'customHeight', 's', 

69 'thickBot', 'thickTop') 

70 r = Alias('index') 

71 s = Alias('style_id') 

72 ht = Float(allow_none=True) 

73 height = Alias('ht') 

74 thickBot = Bool() 

75 thickTop = Bool() 

76 

77 def __init__(self, 

78 worksheet, 

79 index=0, 

80 ht=None, 

81 customHeight=None, # do not write 

82 s=None, 

83 customFormat=None, # do not write 

84 hidden=False, 

85 outlineLevel=0, 

86 outline_level=None, 

87 collapsed=False, 

88 visible=None, 

89 height=None, 

90 r=None, 

91 spans=None, 

92 thickBot=None, 

93 thickTop=None, 

94 **kw 

95 ): 

96 if r is not None: 

97 index = r 

98 if height is not None: 

99 ht = height 

100 self.ht = ht 

101 if visible is not None: 

102 hidden = not visible 

103 if outline_level is not None: 

104 outlineLevel = outline_level 

105 self.thickBot = thickBot 

106 self.thickTop = thickTop 

107 super(RowDimension, self).__init__(index, hidden, outlineLevel, 

108 collapsed, worksheet, style=s) 

109 

110 @property 

111 def customFormat(self): 

112 """Always true if there is a style for the row""" 

113 return self.has_style 

114 

115 @property 

116 def customHeight(self): 

117 """Always true if there is a height for the row""" 

118 return self.ht is not None 

119 

120 

121class ColumnDimension(Dimension): 

122 """Information about the display properties of a column.""" 

123 

124 width = Float() 

125 bestFit = Bool() 

126 auto_size = Alias('bestFit') 

127 index = String() 

128 min = Integer(allow_none=True) 

129 max = Integer(allow_none=True) 

130 collapsed = Bool() 

131 

132 __fields__ = Dimension.__fields__ + ('width', 'bestFit', 'customWidth', 'style', 

133 'min', 'max') 

134 

135 def __init__(self, 

136 worksheet, 

137 index='A', 

138 width=DEFAULT_COLUMN_WIDTH, 

139 bestFit=False, 

140 hidden=False, 

141 outlineLevel=0, 

142 outline_level=None, 

143 collapsed=False, 

144 style=None, 

145 min=None, 

146 max=None, 

147 customWidth=False, # do not write 

148 visible=None, 

149 auto_size=None,): 

150 self.width = width 

151 self.min = min 

152 self.max = max 

153 if visible is not None: 

154 hidden = not visible 

155 if auto_size is not None: 

156 bestFit = auto_size 

157 self.bestFit = bestFit 

158 if outline_level is not None: 

159 outlineLevel = outline_level 

160 self.collapsed = collapsed 

161 super(ColumnDimension, self).__init__(index, hidden, outlineLevel, 

162 collapsed, worksheet, style=style) 

163 

164 

165 @property 

166 def customWidth(self): 

167 """Always true if there is a width for the column""" 

168 return bool(self.width) 

169 

170 

171 def reindex(self): 

172 """ 

173 Set boundaries for column definition 

174 """ 

175 if not all([self.min, self.max]): 

176 self.min = self.max = column_index_from_string(self.index) 

177 

178 

179 def to_tree(self): 

180 attrs = dict(self) 

181 if attrs.keys() != {'min', 'max'}: 

182 return Element("col", **attrs) 

183 

184 

185class DimensionHolder(BoundDictionary): 

186 """ 

187 Allow columns to be grouped 

188 """ 

189 

190 def __init__(self, worksheet, reference="index", default_factory=None): 

191 self.worksheet = worksheet 

192 self.max_outline = None 

193 self.default_factory = default_factory 

194 super(DimensionHolder, self).__init__(reference, default_factory) 

195 

196 

197 def group(self, start, end=None, outline_level=1, hidden=False): 

198 """allow grouping a range of consecutive rows or columns together 

199 

200 :param start: first row or column to be grouped (mandatory) 

201 :param end: last row or column to be grouped (optional, default to start) 

202 :param outline_level: outline level 

203 :param hidden: should the group be hidden on workbook open or not 

204 """ 

205 if end is None: 

206 end = start 

207 

208 if isinstance(self.default_factory(), ColumnDimension): 

209 new_dim = self[start] 

210 new_dim.outline_level = outline_level 

211 new_dim.hidden = hidden 

212 work_sequence = get_column_interval(start, end)[1:] 

213 for column_letter in work_sequence: 

214 if column_letter in self: 

215 del self[column_letter] 

216 new_dim.min, new_dim.max = map(column_index_from_string, (start, end)) 

217 elif isinstance(self.default_factory(), RowDimension): 

218 for el in range(start, end + 1): 

219 new_dim = self.worksheet.row_dimensions[el] 

220 new_dim.outline_level = outline_level 

221 new_dim.hidden = hidden 

222 

223 

224 def to_tree(self): 

225 

226 def sorter(value): 

227 value.reindex() 

228 return value.min 

229 

230 el = Element('cols') 

231 outlines = set() 

232 

233 for col in sorted(self.values(), key=sorter): 

234 obj = col.to_tree() 

235 if obj is not None: 

236 outlines.add(col.outlineLevel) 

237 el.append(obj) 

238 

239 if outlines: 

240 self.max_outline = max(outlines) 

241 

242 if len(el): 

243 return el # must have at least one child 

244 

245 

246class SheetFormatProperties(Serialisable): 

247 

248 tagname = "sheetFormatPr" 

249 

250 baseColWidth = Integer(allow_none=True) 

251 defaultColWidth = Float(allow_none=True) 

252 defaultRowHeight = Float() 

253 customHeight = Bool(allow_none=True) 

254 zeroHeight = Bool(allow_none=True) 

255 thickTop = Bool(allow_none=True) 

256 thickBottom = Bool(allow_none=True) 

257 outlineLevelRow = Integer(allow_none=True) 

258 outlineLevelCol = Integer(allow_none=True) 

259 

260 def __init__(self, 

261 baseColWidth=8, #according to spec 

262 defaultColWidth=None, 

263 defaultRowHeight=15, 

264 customHeight=None, 

265 zeroHeight=None, 

266 thickTop=None, 

267 thickBottom=None, 

268 outlineLevelRow=None, 

269 outlineLevelCol=None, 

270 ): 

271 self.baseColWidth = baseColWidth 

272 self.defaultColWidth = defaultColWidth 

273 self.defaultRowHeight = defaultRowHeight 

274 self.customHeight = customHeight 

275 self.zeroHeight = zeroHeight 

276 self.thickTop = thickTop 

277 self.thickBottom = thickBottom 

278 self.outlineLevelRow = outlineLevelRow 

279 self.outlineLevelCol = outlineLevelCol 

280 

281 

282class SheetDimension(Serialisable): 

283 

284 tagname = "dimension" 

285 

286 ref = String() 

287 

288 def __init__(self, 

289 ref=None, 

290 ): 

291 self.ref = ref 

292 

293 

294 @property 

295 def boundaries(self): 

296 return range_boundaries(self.ref)