Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/xlwt/Row.py: 14%

209 statements  

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

1# -*- coding: windows-1252 -*- 

2 

3from decimal import Decimal 

4from . import BIFFRecords 

5from . import Style 

6from .Cell import StrCell, BlankCell, NumberCell, FormulaCell, MulBlankCell, BooleanCell, ErrorCell, \ 

7 _get_cells_biff_data_mul 

8from . import ExcelFormula 

9import datetime as dt 

10from .Formatting import Font 

11from .compat import basestring, xrange, int_types, iteritems 

12 

13 

14class Row(object): 

15 __slots__ = [# private variables 

16 "__idx", 

17 "__parent", 

18 "__parent_wb", 

19 "__cells", 

20 "__min_col_idx", 

21 "__max_col_idx", 

22 "__xf_index", 

23 "__has_default_xf_index", 

24 "__height_in_pixels", 

25 # public variables 

26 "height", 

27 "has_default_height", 

28 "height_mismatch", 

29 "level", 

30 "collapse", 

31 "hidden", 

32 "space_above", 

33 "space_below"] 

34 

35 def __init__(self, rowx, parent_sheet): 

36 if not (isinstance(rowx, int_types) and 0 <= rowx <= 65535): 

37 raise ValueError("row index was %r, not allowed by .xls format" % rowx) 

38 self.__idx = rowx 

39 self.__parent = parent_sheet 

40 self.__parent_wb = parent_sheet.get_parent() 

41 self.__cells = {} 

42 self.__min_col_idx = 0 

43 self.__max_col_idx = 0 

44 self.__xf_index = 0x0F 

45 self.__has_default_xf_index = 0 

46 self.__height_in_pixels = 0x11 

47 

48 self.height = 0x00FF 

49 self.has_default_height = 0x00 

50 self.height_mismatch = 0 

51 self.level = 0 

52 self.collapse = 0 

53 self.hidden = 0 

54 self.space_above = 0 

55 self.space_below = 0 

56 

57 

58 def __adjust_height(self, style): 

59 twips = style.font.height 

60 points = float(twips)/20.0 

61 # Cell height in pixels can be calcuted by following approx. formula: 

62 # cell height in pixels = font height in points * 83/50 + 2/5 

63 # It works when screen resolution is 96 dpi 

64 pix = int(round(points*83.0/50.0 + 2.0/5.0)) 

65 if pix > self.__height_in_pixels: 

66 self.__height_in_pixels = pix 

67 

68 

69 def __adjust_bound_col_idx(self, *args): 

70 for arg in args: 

71 iarg = int(arg) 

72 if not ((0 <= iarg <= 255) and arg == iarg): 

73 raise ValueError("column index (%r) not an int in range(256)" % arg) 

74 sheet = self.__parent 

75 if iarg < self.__min_col_idx: 

76 self.__min_col_idx = iarg 

77 if iarg > self.__max_col_idx: 

78 self.__max_col_idx = iarg 

79 if iarg < sheet.first_used_col: 

80 sheet.first_used_col = iarg 

81 if iarg > sheet.last_used_col: 

82 sheet.last_used_col = iarg 

83 

84 def __excel_date_dt(self, date): 

85 adj = False 

86 if isinstance(date, dt.date): 

87 if self.__parent_wb.dates_1904: 

88 epoch_tuple = (1904, 1, 1) 

89 else: 

90 epoch_tuple = (1899, 12, 31) 

91 adj = True 

92 if isinstance(date, dt.datetime): 

93 epoch = dt.datetime(*epoch_tuple) 

94 else: 

95 epoch = dt.date(*epoch_tuple) 

96 else: # it's a datetime.time instance 

97 date = dt.datetime.combine(dt.datetime(1900, 1, 1), date) 

98 epoch = dt.datetime(1900, 1, 1) 

99 delta = date - epoch 

100 xldate = delta.days + delta.seconds / 86400.0 

101 # Add a day for Excel's missing leap day in 1900 

102 if adj and xldate > 59: 

103 xldate += 1 

104 return xldate 

105 

106 def get_height_in_pixels(self): 

107 return self.__height_in_pixels 

108 

109 

110 def set_style(self, style): 

111 self.__adjust_height(style) 

112 self.__xf_index = self.__parent_wb.add_style(style) 

113 self.__has_default_xf_index = 1 

114 

115 

116 def get_xf_index(self): 

117 return self.__xf_index 

118 

119 

120 def get_cells_count(self): 

121 return len(self.__cells) 

122 

123 

124 def get_min_col(self): 

125 return self.__min_col_idx 

126 

127 

128 def get_max_col(self): 

129 return self.__max_col_idx 

130 

131 

132 def get_row_biff_data(self): 

133 height_options = (self.height & 0x07FFF) 

134 height_options |= (self.has_default_height & 0x01) << 15 

135 

136 options = (self.level & 0x07) << 0 

137 options |= (self.collapse & 0x01) << 4 

138 options |= (self.hidden & 0x01) << 5 

139 options |= (self.height_mismatch & 0x01) << 6 

140 options |= (self.__has_default_xf_index & 0x01) << 7 

141 options |= (0x01 & 0x01) << 8 

142 options |= (self.__xf_index & 0x0FFF) << 16 

143 options |= (self.space_above & 1) << 28 

144 options |= (self.space_below & 1) << 29 

145 

146 return BIFFRecords.RowRecord(self.__idx, self.__min_col_idx, 

147 self.__max_col_idx, height_options, options).get() 

148 

149 def insert_cell(self, col_index, cell_obj): 

150 if col_index in self.__cells: 

151 if not self.__parent._cell_overwrite_ok: 

152 msg = "Attempt to overwrite cell: sheetname=%r rowx=%d colx=%d" \ 

153 % (self.__parent.name, self.__idx, col_index) 

154 raise Exception(msg) 

155 prev_cell_obj = self.__cells[col_index] 

156 sst_idx = getattr(prev_cell_obj, 'sst_idx', None) 

157 if sst_idx is not None: 

158 self.__parent_wb.del_str(sst_idx) 

159 self.__cells[col_index] = cell_obj 

160 

161 def insert_mulcells(self, colx1, colx2, cell_obj): 

162 self.insert_cell(colx1, cell_obj) 

163 for col_index in xrange(colx1+1, colx2+1): 

164 self.insert_cell(col_index, None) 

165 

166 def get_cells_biff_data(self): 

167 cell_items = [item for item in iteritems(self.__cells) if item[1] is not None] 

168 cell_items.sort() # in column order 

169 return _get_cells_biff_data_mul(self.__idx, cell_items) 

170 # previously: 

171 # return ''.join([cell.get_biff_data() for colx, cell in cell_items]) 

172 

173 def get_index(self): 

174 return self.__idx 

175 

176 def set_cell_text(self, colx, value, style=Style.default_style): 

177 self.__adjust_height(style) 

178 self.__adjust_bound_col_idx(colx) 

179 xf_index = self.__parent_wb.add_style(style) 

180 self.insert_cell(colx, StrCell(self.__idx, colx, xf_index, self.__parent_wb.add_str(value))) 

181 

182 def set_cell_blank(self, colx, style=Style.default_style): 

183 self.__adjust_height(style) 

184 self.__adjust_bound_col_idx(colx) 

185 xf_index = self.__parent_wb.add_style(style) 

186 self.insert_cell(colx, BlankCell(self.__idx, colx, xf_index)) 

187 

188 def set_cell_mulblanks(self, first_colx, last_colx, style=Style.default_style): 

189 assert 0 <= first_colx <= last_colx <= 255 

190 self.__adjust_height(style) 

191 self.__adjust_bound_col_idx(first_colx, last_colx) 

192 xf_index = self.__parent_wb.add_style(style) 

193 # ncols = last_colx - first_colx + 1 

194 self.insert_mulcells(first_colx, last_colx, MulBlankCell(self.__idx, first_colx, last_colx, xf_index)) 

195 

196 def set_cell_number(self, colx, number, style=Style.default_style): 

197 self.__adjust_height(style) 

198 self.__adjust_bound_col_idx(colx) 

199 xf_index = self.__parent_wb.add_style(style) 

200 self.insert_cell(colx, NumberCell(self.__idx, colx, xf_index, number)) 

201 

202 def set_cell_date(self, colx, datetime_obj, style=Style.default_style): 

203 self.__adjust_height(style) 

204 self.__adjust_bound_col_idx(colx) 

205 xf_index = self.__parent_wb.add_style(style) 

206 self.insert_cell(colx, 

207 NumberCell(self.__idx, colx, xf_index, self.__excel_date_dt(datetime_obj))) 

208 

209 def set_cell_formula(self, colx, formula, style=Style.default_style, calc_flags=0): 

210 self.__adjust_height(style) 

211 self.__adjust_bound_col_idx(colx) 

212 xf_index = self.__parent_wb.add_style(style) 

213 self.__parent_wb.add_sheet_reference(formula) 

214 self.insert_cell(colx, FormulaCell(self.__idx, colx, xf_index, formula, calc_flags=0)) 

215 

216 def set_cell_boolean(self, colx, value, style=Style.default_style): 

217 self.__adjust_height(style) 

218 self.__adjust_bound_col_idx(colx) 

219 xf_index = self.__parent_wb.add_style(style) 

220 self.insert_cell(colx, BooleanCell(self.__idx, colx, xf_index, bool(value))) 

221 

222 def set_cell_error(self, colx, error_string_or_code, style=Style.default_style): 

223 self.__adjust_height(style) 

224 self.__adjust_bound_col_idx(colx) 

225 xf_index = self.__parent_wb.add_style(style) 

226 self.insert_cell(colx, ErrorCell(self.__idx, colx, xf_index, error_string_or_code)) 

227 

228 def write(self, col, label, style=Style.default_style): 

229 self.__adjust_height(style) 

230 self.__adjust_bound_col_idx(col) 

231 style_index = self.__parent_wb.add_style(style) 

232 if isinstance(label, basestring): 

233 if len(label) > 0: 

234 self.insert_cell(col, 

235 StrCell(self.__idx, col, style_index, self.__parent_wb.add_str(label)) 

236 ) 

237 else: 

238 self.insert_cell(col, BlankCell(self.__idx, col, style_index)) 

239 elif isinstance(label, bool): # bool is subclass of int; test bool first 

240 self.insert_cell(col, BooleanCell(self.__idx, col, style_index, label)) 

241 elif isinstance(label, int_types+(float, Decimal)): 

242 self.insert_cell(col, NumberCell(self.__idx, col, style_index, label)) 

243 elif isinstance(label, (dt.datetime, dt.date, dt.time)): 

244 date_number = self.__excel_date_dt(label) 

245 self.insert_cell(col, NumberCell(self.__idx, col, style_index, date_number)) 

246 elif label is None: 

247 self.insert_cell(col, BlankCell(self.__idx, col, style_index)) 

248 elif isinstance(label, ExcelFormula.Formula): 

249 self.__parent_wb.add_sheet_reference(label) 

250 self.insert_cell(col, FormulaCell(self.__idx, col, style_index, label)) 

251 elif isinstance(label, (list, tuple)): 

252 self.__rich_text_helper(col, label, style, style_index) 

253 else: 

254 raise Exception("Unexpected data type %r" % type(label)) 

255 

256 def set_cell_rich_text(self, col, rich_text_list, style=Style.default_style): 

257 self.__adjust_height(style) 

258 self.__adjust_bound_col_idx(col) 

259 if not isinstance(rich_text_list, (list, tuple)): 

260 raise Exception("Unexpected data type %r" % type(rich_text_list)) 

261 self.__rich_text_helper(col, rich_text_list, style) 

262 

263 def __rich_text_helper(self, col, rich_text_list, style, style_index=None): 

264 if style_index is None: 

265 style_index = self.__parent_wb.add_style(style) 

266 default_font = None 

267 rt = [] 

268 for data in rich_text_list: 

269 if isinstance(data, basestring): 

270 s = data 

271 font = default_font 

272 elif isinstance(data, (list, tuple)): 

273 if not isinstance(data[0], basestring) or not isinstance(data[1], Font): 

274 raise Exception ("Unexpected data type %r, %r" % (type(data[0]), type(data[1]))) 

275 s = data[0] 

276 font = self.__parent_wb.add_font(data[1]) 

277 else: 

278 raise Exception ("Unexpected data type %r" % type(data)) 

279 if s: 

280 rt.append((s, font)) 

281 if default_font is None: 

282 default_font = self.__parent_wb.add_font(style.font) 

283 if rt: 

284 self.insert_cell(col, StrCell(self.__idx, col, style_index, self.__parent_wb.add_rt(rt))) 

285 else: 

286 self.insert_cell(col, BlankCell(self.__idx, col, style_index)) 

287 

288 write_blanks = set_cell_mulblanks 

289 write_rich_text = set_cell_rich_text 

290 

291 

292 

293