Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/openpyxl/cell/_writer.py: 10%
72 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1# Copyright (c) 2010-2022 openpyxl
3from openpyxl.compat import safe_string
4from openpyxl.xml.functions import Element, SubElement, whitespace, XML_NS, REL_NS
5from openpyxl import LXML
6from openpyxl.utils.datetime import to_excel, to_ISO8601
7from datetime import timedelta
10def _set_attributes(cell, styled=None):
11 """
12 Set coordinate and datatype
13 """
14 coordinate = cell.coordinate
15 attrs = {'r': coordinate}
16 if styled:
17 attrs['s'] = f"{cell.style_id}"
19 if cell.data_type == "s":
20 attrs['t'] = "inlineStr"
21 elif cell.data_type != 'f':
22 attrs['t'] = cell.data_type
24 value = cell._value
26 if cell.data_type == "d":
27 if hasattr(value, "tzinfo") and value.tzinfo is not None:
28 raise TypeError("Excel does not support timezones in datetimes. "
29 "The tzinfo in the datetime/time object must be set to None.")
31 if cell.parent.parent.iso_dates and not isinstance(value, timedelta):
32 value = to_ISO8601(value)
33 else:
34 attrs['t'] = "n"
35 value = to_excel(value, cell.parent.parent.epoch)
37 if cell.hyperlink:
38 cell.parent._hyperlinks.append(cell.hyperlink)
40 return value, attrs
43def etree_write_cell(xf, worksheet, cell, styled=None):
45 value, attributes = _set_attributes(cell, styled)
47 el = Element("c", attributes)
48 if value is None or value == "":
49 xf.write(el)
50 return
52 if cell.data_type == 'f':
53 shared_formula = worksheet.formula_attributes.get(cell.coordinate, {})
54 formula = SubElement(el, 'f', shared_formula)
55 if value is not None:
56 formula.text = value[1:]
57 value = None
59 if cell.data_type == 's':
60 inline_string = SubElement(el, 'is')
61 text = SubElement(inline_string, 't')
62 text.text = value
63 whitespace(text)
66 else:
67 cell_content = SubElement(el, 'v')
68 if value is not None:
69 cell_content.text = safe_string(value)
71 xf.write(el)
74def lxml_write_cell(xf, worksheet, cell, styled=False):
75 value, attributes = _set_attributes(cell, styled)
77 if value == '' or value is None:
78 with xf.element("c", attributes):
79 return
81 with xf.element('c', attributes):
82 if cell.data_type == 'f':
83 shared_formula = worksheet.formula_attributes.get(cell.coordinate, {})
84 with xf.element('f', shared_formula):
85 if value is not None:
86 xf.write(value[1:])
87 value = None
89 if cell.data_type == 's':
90 with xf.element("is"):
91 attrs = {}
92 if value != value.strip():
93 attrs["{%s}space" % XML_NS] = "preserve"
94 el = Element("t", attrs) # lxml can't handle xml-ns
95 el.text = value
96 xf.write(el)
97 #with xf.element("t", attrs):
98 #xf.write(value)
99 else:
100 with xf.element("v"):
101 if value is not None:
102 xf.write(safe_string(value))
105if LXML: 105 ↛ 106line 105 didn't jump to line 106, because the condition on line 105 was never true
106 write_cell = lxml_write_cell
107else:
108 write_cell = etree_write_cell