Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/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#
7# Copyright (c) 2013-2025, John McNamara, jmcnamara@cpan.org
8#
10from datetime import datetime, timezone
11from typing import Dict, Union
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().__init__()
37 self.properties = {}
38 self.iso_date = ""
40 ###########################################################################
41 #
42 # Private API.
43 #
44 ###########################################################################
46 def _assemble_xml_file(self):
47 # Assemble and write the XML file.
49 # Set the creation date for the file.
50 date = self.properties.get("created")
51 if not isinstance(date, datetime):
52 date = datetime.now(timezone.utc)
54 self.iso_date = date.strftime("%Y-%m-%dT%H:%M:%SZ")
56 # Write the XML declaration.
57 self._xml_declaration()
59 self._write_cp_core_properties()
60 self._write_dc_title()
61 self._write_dc_subject()
62 self._write_dc_creator()
63 self._write_cp_keywords()
64 self._write_dc_description()
65 self._write_cp_last_modified_by()
66 self._write_dcterms_created()
67 self._write_dcterms_modified()
68 self._write_cp_category()
69 self._write_cp_content_status()
71 self._xml_end_tag("cp:coreProperties")
73 # Close the file.
74 self._xml_close()
76 def _set_properties(self, properties: Dict[str, Union[str, datetime]]):
77 # Set the document properties.
78 self.properties = properties
80 ###########################################################################
81 #
82 # XML methods.
83 #
84 ###########################################################################
86 def _write_cp_core_properties(self):
87 # Write the <cp:coreProperties> element.
89 xmlns_cp = (
90 "http://schemas.openxmlformats.org/package/2006/"
91 + "metadata/core-properties"
92 )
93 xmlns_dc = "http://purl.org/dc/elements/1.1/"
94 xmlns_dcterms = "http://purl.org/dc/terms/"
95 xmlns_dcmitype = "http://purl.org/dc/dcmitype/"
96 xmlns_xsi = "http://www.w3.org/2001/XMLSchema-instance"
98 attributes = [
99 ("xmlns:cp", xmlns_cp),
100 ("xmlns:dc", xmlns_dc),
101 ("xmlns:dcterms", xmlns_dcterms),
102 ("xmlns:dcmitype", xmlns_dcmitype),
103 ("xmlns:xsi", xmlns_xsi),
104 ]
106 self._xml_start_tag("cp:coreProperties", attributes)
108 def _write_dc_creator(self):
109 # Write the <dc:creator> element.
110 data = self.properties.get("author", "")
112 self._xml_data_element("dc:creator", data)
114 def _write_cp_last_modified_by(self):
115 # Write the <cp:lastModifiedBy> element.
116 data = self.properties.get("author", "")
118 self._xml_data_element("cp:lastModifiedBy", data)
120 def _write_dcterms_created(self):
121 # Write the <dcterms:created> element.
122 attributes = [("xsi:type", "dcterms:W3CDTF")]
123 self._xml_data_element("dcterms:created", self.iso_date, attributes)
125 def _write_dcterms_modified(self):
126 # Write the <dcterms:modified> element.
127 attributes = [("xsi:type", "dcterms:W3CDTF")]
128 self._xml_data_element("dcterms:modified", self.iso_date, attributes)
130 def _write_dc_title(self):
131 # Write the <dc:title> element.
132 if "title" in self.properties:
133 data = self.properties["title"]
134 else:
135 return
137 self._xml_data_element("dc:title", data)
139 def _write_dc_subject(self):
140 # Write the <dc:subject> element.
141 if "subject" in self.properties:
142 data = self.properties["subject"]
143 else:
144 return
146 self._xml_data_element("dc:subject", data)
148 def _write_cp_keywords(self):
149 # Write the <cp:keywords> element.
150 if "keywords" in self.properties:
151 data = self.properties["keywords"]
152 else:
153 return
155 self._xml_data_element("cp:keywords", data)
157 def _write_dc_description(self):
158 # Write the <dc:description> element.
159 if "comments" in self.properties:
160 data = self.properties["comments"]
161 else:
162 return
164 self._xml_data_element("dc:description", data)
166 def _write_cp_category(self):
167 # Write the <cp:category> element.
168 if "category" in self.properties:
169 data = self.properties["category"]
170 else:
171 return
173 self._xml_data_element("cp:category", data)
175 def _write_cp_content_status(self):
176 # Write the <cp:contentStatus> element.
177 if "status" in self.properties:
178 data = self.properties["status"]
179 else:
180 return
182 self._xml_data_element("cp:contentStatus", data)