Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/xlsxwriter/table.py: 14%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

100 statements  

1############################################################################### 

2# 

3# Table - A class for writing the Excel XLSX Worksheet file. 

4# 

5# SPDX-License-Identifier: BSD-2-Clause 

6# 

7# Copyright (c) 2013-2025, John McNamara, jmcnamara@cpan.org 

8# 

9 

10from . import xmlwriter 

11 

12 

13class Table(xmlwriter.XMLwriter): 

14 """ 

15 A class for writing the Excel XLSX Table file. 

16 

17 

18 """ 

19 

20 ########################################################################### 

21 # 

22 # Public API. 

23 # 

24 ########################################################################### 

25 

26 def __init__(self) -> None: 

27 """ 

28 Constructor. 

29 

30 """ 

31 

32 super().__init__() 

33 

34 self.properties = {} 

35 

36 ########################################################################### 

37 # 

38 # Private API. 

39 # 

40 ########################################################################### 

41 

42 def _assemble_xml_file(self) -> None: 

43 # Assemble and write the XML file. 

44 

45 # Write the XML declaration. 

46 self._xml_declaration() 

47 

48 # Write the table element. 

49 self._write_table() 

50 

51 # Write the autoFilter element. 

52 self._write_auto_filter() 

53 

54 # Write the tableColumns element. 

55 self._write_table_columns() 

56 

57 # Write the tableStyleInfo element. 

58 self._write_table_style_info() 

59 

60 # Write the extLst element for alt text/title. 

61 self._write_ext_lst() 

62 

63 # Close the table tag. 

64 self._xml_end_tag("table") 

65 

66 # Close the file. 

67 self._xml_close() 

68 

69 def _set_properties(self, properties) -> None: 

70 # Set the document properties. 

71 self.properties = properties 

72 

73 ########################################################################### 

74 # 

75 # XML methods. 

76 # 

77 ########################################################################### 

78 

79 def _write_table(self) -> None: 

80 # Write the <table> element. 

81 schema = "http://schemas.openxmlformats.org/" 

82 xmlns = schema + "spreadsheetml/2006/main" 

83 table_id = self.properties["id"] 

84 name = self.properties["name"] 

85 display_name = self.properties["name"] 

86 ref = self.properties["range"] 

87 totals_row_shown = self.properties["totals_row_shown"] 

88 header_row_count = self.properties["header_row_count"] 

89 

90 attributes = [ 

91 ("xmlns", xmlns), 

92 ("id", table_id), 

93 ("name", name), 

94 ("displayName", display_name), 

95 ("ref", ref), 

96 ] 

97 

98 if not header_row_count: 

99 attributes.append(("headerRowCount", 0)) 

100 

101 if totals_row_shown: 

102 attributes.append(("totalsRowCount", 1)) 

103 else: 

104 attributes.append(("totalsRowShown", 0)) 

105 

106 self._xml_start_tag("table", attributes) 

107 

108 def _write_auto_filter(self) -> None: 

109 # Write the <autoFilter> element. 

110 autofilter = self.properties.get("autofilter", 0) 

111 

112 if not autofilter: 

113 return 

114 

115 attributes = [ 

116 ( 

117 "ref", 

118 autofilter, 

119 ) 

120 ] 

121 

122 self._xml_empty_tag("autoFilter", attributes) 

123 

124 def _write_table_columns(self) -> None: 

125 # Write the <tableColumns> element. 

126 columns = self.properties["columns"] 

127 

128 count = len(columns) 

129 

130 attributes = [("count", count)] 

131 

132 self._xml_start_tag("tableColumns", attributes) 

133 

134 for col_data in columns: 

135 # Write the tableColumn element. 

136 self._write_table_column(col_data) 

137 

138 self._xml_end_tag("tableColumns") 

139 

140 def _write_table_column(self, col_data) -> None: 

141 # Write the <tableColumn> element. 

142 attributes = [ 

143 ("id", col_data["id"]), 

144 ("name", col_data["name"]), 

145 ] 

146 

147 if col_data.get("total_string"): 

148 attributes.append(("totalsRowLabel", col_data["total_string"])) 

149 elif col_data.get("total_function"): 

150 attributes.append(("totalsRowFunction", col_data["total_function"])) 

151 

152 if "format" in col_data and col_data["format"] is not None: 

153 attributes.append(("dataDxfId", col_data["format"])) 

154 

155 if col_data.get("formula") or col_data.get("custom_total"): 

156 self._xml_start_tag("tableColumn", attributes) 

157 

158 if col_data.get("formula"): 

159 # Write the calculatedColumnFormula element. 

160 self._write_calculated_column_formula(col_data["formula"]) 

161 

162 if col_data.get("custom_total"): 

163 # Write the totalsRowFormula element. 

164 self._write_totals_row_formula(col_data.get("custom_total")) 

165 

166 self._xml_end_tag("tableColumn") 

167 else: 

168 self._xml_empty_tag("tableColumn", attributes) 

169 

170 def _write_table_style_info(self) -> None: 

171 # Write the <tableStyleInfo> element. 

172 props = self.properties 

173 attributes = [] 

174 

175 name = props["style"] 

176 show_first_column = 0 + props["show_first_col"] 

177 show_last_column = 0 + props["show_last_col"] 

178 show_row_stripes = 0 + props["show_row_stripes"] 

179 show_column_stripes = 0 + props["show_col_stripes"] 

180 

181 if name is not None and name != "" and name != "None": 

182 attributes.append(("name", name)) 

183 

184 attributes.append(("showFirstColumn", show_first_column)) 

185 attributes.append(("showLastColumn", show_last_column)) 

186 attributes.append(("showRowStripes", show_row_stripes)) 

187 attributes.append(("showColumnStripes", show_column_stripes)) 

188 

189 self._xml_empty_tag("tableStyleInfo", attributes) 

190 

191 def _write_calculated_column_formula(self, formula) -> None: 

192 # Write the <calculatedColumnFormula> element. 

193 self._xml_data_element("calculatedColumnFormula", formula) 

194 

195 def _write_totals_row_formula(self, formula) -> None: 

196 # Write the <totalsRowFormula> element. 

197 self._xml_data_element("totalsRowFormula", formula) 

198 

199 def _write_ext_lst(self) -> None: 

200 # Write the <extLst> element for the alt text/description. 

201 props = self.properties 

202 

203 if not props.get("description") and not props.get("title"): 

204 return 

205 

206 attributes = [ 

207 ("uri", "{504A1905-F514-4f6f-8877-14C23A59335A}"), 

208 ( 

209 "xmlns:x14", 

210 "http://schemas.microsoft.com/office/spreadsheetml/2009/9/main", 

211 ), 

212 ] 

213 

214 self._xml_start_tag("extLst") 

215 self._xml_start_tag("ext", attributes) 

216 

217 # Write the x14:table element. 

218 self._write_x14_table() 

219 

220 self._xml_end_tag("ext") 

221 self._xml_end_tag("extLst") 

222 

223 def _write_x14_table(self): 

224 # Write the <x14:table> element. 

225 props = self.properties 

226 attributes = [] 

227 

228 if props.get("title"): 

229 attributes.append(("altText", props["title"])) 

230 

231 if props.get("description"): 

232 attributes.append(("altTextSummary", props["description"])) 

233 

234 self._xml_empty_tag("x14:table", attributes)