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

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

80 statements  

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

2# 

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

4# 

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

6# Copyright 2013-2024, John McNamara, jmcnamara@cpan.org 

7# 

8 

9from . import xmlwriter 

10 

11 

12class Table(xmlwriter.XMLwriter): 

13 """ 

14 A class for writing the Excel XLSX Table file. 

15 

16 

17 """ 

18 

19 ########################################################################### 

20 # 

21 # Public API. 

22 # 

23 ########################################################################### 

24 

25 def __init__(self): 

26 """ 

27 Constructor. 

28 

29 """ 

30 

31 super(Table, self).__init__() 

32 

33 self.properties = {} 

34 

35 ########################################################################### 

36 # 

37 # Private API. 

38 # 

39 ########################################################################### 

40 

41 def _assemble_xml_file(self): 

42 # Assemble and write the XML file. 

43 

44 # Write the XML declaration. 

45 self._xml_declaration() 

46 

47 # Write the table element. 

48 self._write_table() 

49 

50 # Write the autoFilter element. 

51 self._write_auto_filter() 

52 

53 # Write the tableColumns element. 

54 self._write_table_columns() 

55 

56 # Write the tableStyleInfo element. 

57 self._write_table_style_info() 

58 

59 # Close the table tag. 

60 self._xml_end_tag("table") 

61 

62 # Close the file. 

63 self._xml_close() 

64 

65 def _set_properties(self, properties): 

66 # Set the document properties. 

67 self.properties = properties 

68 

69 ########################################################################### 

70 # 

71 # XML methods. 

72 # 

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

74 

75 def _write_table(self): 

76 # Write the <table> element. 

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

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

79 table_id = self.properties["id"] 

80 name = self.properties["name"] 

81 display_name = self.properties["name"] 

82 ref = self.properties["range"] 

83 totals_row_shown = self.properties["totals_row_shown"] 

84 header_row_count = self.properties["header_row_count"] 

85 

86 attributes = [ 

87 ("xmlns", xmlns), 

88 ("id", table_id), 

89 ("name", name), 

90 ("displayName", display_name), 

91 ("ref", ref), 

92 ] 

93 

94 if not header_row_count: 

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

96 

97 if totals_row_shown: 

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

99 else: 

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

101 

102 self._xml_start_tag("table", attributes) 

103 

104 def _write_auto_filter(self): 

105 # Write the <autoFilter> element. 

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

107 

108 if not autofilter: 

109 return 

110 

111 attributes = [ 

112 ( 

113 "ref", 

114 autofilter, 

115 ) 

116 ] 

117 

118 self._xml_empty_tag("autoFilter", attributes) 

119 

120 def _write_table_columns(self): 

121 # Write the <tableColumns> element. 

122 columns = self.properties["columns"] 

123 

124 count = len(columns) 

125 

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

127 

128 self._xml_start_tag("tableColumns", attributes) 

129 

130 for col_data in columns: 

131 # Write the tableColumn element. 

132 self._write_table_column(col_data) 

133 

134 self._xml_end_tag("tableColumns") 

135 

136 def _write_table_column(self, col_data): 

137 # Write the <tableColumn> element. 

138 attributes = [ 

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

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

141 ] 

142 

143 if col_data.get("total_string"): 

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

145 elif col_data.get("total_function"): 

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

147 

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

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

150 

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

152 self._xml_start_tag("tableColumn", attributes) 

153 

154 if col_data.get("formula"): 

155 # Write the calculatedColumnFormula element. 

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

157 

158 if col_data.get("custom_total"): 

159 # Write the totalsRowFormula element. 

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

161 

162 self._xml_end_tag("tableColumn") 

163 else: 

164 self._xml_empty_tag("tableColumn", attributes) 

165 

166 def _write_table_style_info(self): 

167 # Write the <tableStyleInfo> element. 

168 props = self.properties 

169 attributes = [] 

170 

171 name = props["style"] 

172 show_first_column = 0 + props["show_first_col"] 

173 show_last_column = 0 + props["show_last_col"] 

174 show_row_stripes = 0 + props["show_row_stripes"] 

175 show_column_stripes = 0 + props["show_col_stripes"] 

176 

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

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

179 

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

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

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

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

184 

185 self._xml_empty_tag("tableStyleInfo", attributes) 

186 

187 def _write_calculated_column_formula(self, formula): 

188 # Write the <calculatedColumnFormula> element. 

189 self._xml_data_element("calculatedColumnFormula", formula) 

190 

191 def _write_totals_row_formula(self, formula): 

192 # Write the <totalsRowFormula> element. 

193 self._xml_data_element("totalsRowFormula", formula)