Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/xlsxwriter/core.py: 84%
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# Core - 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#
9# Standard packages.
10from datetime import datetime, timezone
12# Package imports.
13from . import xmlwriter
16class Core(xmlwriter.XMLwriter):
17 """
18 A class for writing the Excel XLSX Core file.
21 """
23 ###########################################################################
24 #
25 # Public API.
26 #
27 ###########################################################################
29 def __init__(self):
30 """
31 Constructor.
33 """
35 super(Core, self).__init__()
37 self.properties = {}
39 ###########################################################################
40 #
41 # Private API.
42 #
43 ###########################################################################
45 def _assemble_xml_file(self):
46 # Assemble and write the XML file.
48 # Write the XML declaration.
49 self._xml_declaration()
51 self._write_cp_core_properties()
52 self._write_dc_title()
53 self._write_dc_subject()
54 self._write_dc_creator()
55 self._write_cp_keywords()
56 self._write_dc_description()
57 self._write_cp_last_modified_by()
58 self._write_dcterms_created()
59 self._write_dcterms_modified()
60 self._write_cp_category()
61 self._write_cp_content_status()
63 self._xml_end_tag("cp:coreProperties")
65 # Close the file.
66 self._xml_close()
68 def _set_properties(self, properties):
69 # Set the document properties.
70 self.properties = properties
72 def _datetime_to_iso8601_date(self, date):
73 # Convert to a ISO 8601 style "2010-01-01T00:00:00Z" date.
74 if not date:
75 date = datetime.now(timezone.utc)
77 return date.strftime("%Y-%m-%dT%H:%M:%SZ")
79 ###########################################################################
80 #
81 # XML methods.
82 #
83 ###########################################################################
85 def _write_cp_core_properties(self):
86 # Write the <cp:coreProperties> element.
88 xmlns_cp = (
89 "http://schemas.openxmlformats.org/package/2006/"
90 + "metadata/core-properties"
91 )
92 xmlns_dc = "http://purl.org/dc/elements/1.1/"
93 xmlns_dcterms = "http://purl.org/dc/terms/"
94 xmlns_dcmitype = "http://purl.org/dc/dcmitype/"
95 xmlns_xsi = "http://www.w3.org/2001/XMLSchema-instance"
97 attributes = [
98 ("xmlns:cp", xmlns_cp),
99 ("xmlns:dc", xmlns_dc),
100 ("xmlns:dcterms", xmlns_dcterms),
101 ("xmlns:dcmitype", xmlns_dcmitype),
102 ("xmlns:xsi", xmlns_xsi),
103 ]
105 self._xml_start_tag("cp:coreProperties", attributes)
107 def _write_dc_creator(self):
108 # Write the <dc:creator> element.
109 data = self.properties.get("author", "")
111 self._xml_data_element("dc:creator", data)
113 def _write_cp_last_modified_by(self):
114 # Write the <cp:lastModifiedBy> element.
115 data = self.properties.get("author", "")
117 self._xml_data_element("cp:lastModifiedBy", data)
119 def _write_dcterms_created(self):
120 # Write the <dcterms:created> element.
121 date = self.properties.get("created", datetime.now(timezone.utc))
123 xsi_type = "dcterms:W3CDTF"
125 date = self._datetime_to_iso8601_date(date)
127 attributes = [
128 (
129 "xsi:type",
130 xsi_type,
131 )
132 ]
134 self._xml_data_element("dcterms:created", date, attributes)
136 def _write_dcterms_modified(self):
137 # Write the <dcterms:modified> element.
138 date = self.properties.get("created", datetime.now(timezone.utc))
140 xsi_type = "dcterms:W3CDTF"
142 date = self._datetime_to_iso8601_date(date)
144 attributes = [
145 (
146 "xsi:type",
147 xsi_type,
148 )
149 ]
151 self._xml_data_element("dcterms:modified", date, attributes)
153 def _write_dc_title(self):
154 # Write the <dc:title> element.
155 if "title" in self.properties:
156 data = self.properties["title"]
157 else:
158 return
160 self._xml_data_element("dc:title", data)
162 def _write_dc_subject(self):
163 # Write the <dc:subject> element.
164 if "subject" in self.properties:
165 data = self.properties["subject"]
166 else:
167 return
169 self._xml_data_element("dc:subject", data)
171 def _write_cp_keywords(self):
172 # Write the <cp:keywords> element.
173 if "keywords" in self.properties:
174 data = self.properties["keywords"]
175 else:
176 return
178 self._xml_data_element("cp:keywords", data)
180 def _write_dc_description(self):
181 # Write the <dc:description> element.
182 if "comments" in self.properties:
183 data = self.properties["comments"]
184 else:
185 return
187 self._xml_data_element("dc:description", data)
189 def _write_cp_category(self):
190 # Write the <cp:category> element.
191 if "category" in self.properties:
192 data = self.properties["category"]
193 else:
194 return
196 self._xml_data_element("cp:category", data)
198 def _write_cp_content_status(self):
199 # Write the <cp:contentStatus> element.
200 if "status" in self.properties:
201 data = self.properties["status"]
202 else:
203 return
205 self._xml_data_element("cp:contentStatus", data)