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
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# Copyright 2013-2024, John McNamara, jmcnamara@cpan.org
7#
9from . import xmlwriter
12class Table(xmlwriter.XMLwriter):
13 """
14 A class for writing the Excel XLSX Table file.
17 """
19 ###########################################################################
20 #
21 # Public API.
22 #
23 ###########################################################################
25 def __init__(self):
26 """
27 Constructor.
29 """
31 super(Table, self).__init__()
33 self.properties = {}
35 ###########################################################################
36 #
37 # Private API.
38 #
39 ###########################################################################
41 def _assemble_xml_file(self):
42 # Assemble and write the XML file.
44 # Write the XML declaration.
45 self._xml_declaration()
47 # Write the table element.
48 self._write_table()
50 # Write the autoFilter element.
51 self._write_auto_filter()
53 # Write the tableColumns element.
54 self._write_table_columns()
56 # Write the tableStyleInfo element.
57 self._write_table_style_info()
59 # Close the table tag.
60 self._xml_end_tag("table")
62 # Close the file.
63 self._xml_close()
65 def _set_properties(self, properties):
66 # Set the document properties.
67 self.properties = properties
69 ###########################################################################
70 #
71 # XML methods.
72 #
73 ###########################################################################
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"]
86 attributes = [
87 ("xmlns", xmlns),
88 ("id", table_id),
89 ("name", name),
90 ("displayName", display_name),
91 ("ref", ref),
92 ]
94 if not header_row_count:
95 attributes.append(("headerRowCount", 0))
97 if totals_row_shown:
98 attributes.append(("totalsRowCount", 1))
99 else:
100 attributes.append(("totalsRowShown", 0))
102 self._xml_start_tag("table", attributes)
104 def _write_auto_filter(self):
105 # Write the <autoFilter> element.
106 autofilter = self.properties.get("autofilter", 0)
108 if not autofilter:
109 return
111 attributes = [
112 (
113 "ref",
114 autofilter,
115 )
116 ]
118 self._xml_empty_tag("autoFilter", attributes)
120 def _write_table_columns(self):
121 # Write the <tableColumns> element.
122 columns = self.properties["columns"]
124 count = len(columns)
126 attributes = [("count", count)]
128 self._xml_start_tag("tableColumns", attributes)
130 for col_data in columns:
131 # Write the tableColumn element.
132 self._write_table_column(col_data)
134 self._xml_end_tag("tableColumns")
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 ]
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"]))
148 if "format" in col_data and col_data["format"] is not None:
149 attributes.append(("dataDxfId", col_data["format"]))
151 if col_data.get("formula") or col_data.get("custom_total"):
152 self._xml_start_tag("tableColumn", attributes)
154 if col_data.get("formula"):
155 # Write the calculatedColumnFormula element.
156 self._write_calculated_column_formula(col_data["formula"])
158 if col_data.get("custom_total"):
159 # Write the totalsRowFormula element.
160 self._write_totals_row_formula(col_data.get("custom_total"))
162 self._xml_end_tag("tableColumn")
163 else:
164 self._xml_empty_tag("tableColumn", attributes)
166 def _write_table_style_info(self):
167 # Write the <tableStyleInfo> element.
168 props = self.properties
169 attributes = []
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"]
177 if name is not None and name != "" and name != "None":
178 attributes.append(("name", name))
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))
185 self._xml_empty_tag("tableStyleInfo", attributes)
187 def _write_calculated_column_formula(self, formula):
188 # Write the <calculatedColumnFormula> element.
189 self._xml_data_element("calculatedColumnFormula", formula)
191 def _write_totals_row_formula(self, formula):
192 # Write the <totalsRowFormula> element.
193 self._xml_data_element("totalsRowFormula", formula)