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
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
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#
10from . import xmlwriter
13class Table(xmlwriter.XMLwriter):
14 """
15 A class for writing the Excel XLSX Table file.
18 """
20 ###########################################################################
21 #
22 # Public API.
23 #
24 ###########################################################################
26 def __init__(self) -> None:
27 """
28 Constructor.
30 """
32 super().__init__()
34 self.properties = {}
36 ###########################################################################
37 #
38 # Private API.
39 #
40 ###########################################################################
42 def _assemble_xml_file(self) -> None:
43 # Assemble and write the XML file.
45 # Write the XML declaration.
46 self._xml_declaration()
48 # Write the table element.
49 self._write_table()
51 # Write the autoFilter element.
52 self._write_auto_filter()
54 # Write the tableColumns element.
55 self._write_table_columns()
57 # Write the tableStyleInfo element.
58 self._write_table_style_info()
60 # Write the extLst element for alt text/title.
61 self._write_ext_lst()
63 # Close the table tag.
64 self._xml_end_tag("table")
66 # Close the file.
67 self._xml_close()
69 def _set_properties(self, properties) -> None:
70 # Set the document properties.
71 self.properties = properties
73 ###########################################################################
74 #
75 # XML methods.
76 #
77 ###########################################################################
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"]
90 attributes = [
91 ("xmlns", xmlns),
92 ("id", table_id),
93 ("name", name),
94 ("displayName", display_name),
95 ("ref", ref),
96 ]
98 if not header_row_count:
99 attributes.append(("headerRowCount", 0))
101 if totals_row_shown:
102 attributes.append(("totalsRowCount", 1))
103 else:
104 attributes.append(("totalsRowShown", 0))
106 self._xml_start_tag("table", attributes)
108 def _write_auto_filter(self) -> None:
109 # Write the <autoFilter> element.
110 autofilter = self.properties.get("autofilter", 0)
112 if not autofilter:
113 return
115 attributes = [
116 (
117 "ref",
118 autofilter,
119 )
120 ]
122 self._xml_empty_tag("autoFilter", attributes)
124 def _write_table_columns(self) -> None:
125 # Write the <tableColumns> element.
126 columns = self.properties["columns"]
128 count = len(columns)
130 attributes = [("count", count)]
132 self._xml_start_tag("tableColumns", attributes)
134 for col_data in columns:
135 # Write the tableColumn element.
136 self._write_table_column(col_data)
138 self._xml_end_tag("tableColumns")
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 ]
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"]))
152 if "format" in col_data and col_data["format"] is not None:
153 attributes.append(("dataDxfId", col_data["format"]))
155 if col_data.get("formula") or col_data.get("custom_total"):
156 self._xml_start_tag("tableColumn", attributes)
158 if col_data.get("formula"):
159 # Write the calculatedColumnFormula element.
160 self._write_calculated_column_formula(col_data["formula"])
162 if col_data.get("custom_total"):
163 # Write the totalsRowFormula element.
164 self._write_totals_row_formula(col_data.get("custom_total"))
166 self._xml_end_tag("tableColumn")
167 else:
168 self._xml_empty_tag("tableColumn", attributes)
170 def _write_table_style_info(self) -> None:
171 # Write the <tableStyleInfo> element.
172 props = self.properties
173 attributes = []
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"]
181 if name is not None and name != "" and name != "None":
182 attributes.append(("name", name))
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))
189 self._xml_empty_tag("tableStyleInfo", attributes)
191 def _write_calculated_column_formula(self, formula) -> None:
192 # Write the <calculatedColumnFormula> element.
193 self._xml_data_element("calculatedColumnFormula", formula)
195 def _write_totals_row_formula(self, formula) -> None:
196 # Write the <totalsRowFormula> element.
197 self._xml_data_element("totalsRowFormula", formula)
199 def _write_ext_lst(self) -> None:
200 # Write the <extLst> element for the alt text/description.
201 props = self.properties
203 if not props.get("description") and not props.get("title"):
204 return
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 ]
214 self._xml_start_tag("extLst")
215 self._xml_start_tag("ext", attributes)
217 # Write the x14:table element.
218 self._write_x14_table()
220 self._xml_end_tag("ext")
221 self._xml_end_tag("extLst")
223 def _write_x14_table(self):
224 # Write the <x14:table> element.
225 props = self.properties
226 attributes = []
228 if props.get("title"):
229 attributes.append(("altText", props["title"]))
231 if props.get("description"):
232 attributes.append(("altTextSummary", props["description"]))
234 self._xml_empty_tag("x14:table", attributes)