def __iter__(self): for attr in self.__attrs__: value = getattr(self, attr) if value and attr != "outline": yield attr, safe_string(value) elif attr == "outline" and not value: yield attr, safe_string(value)
def __iter__(self): for attr in self.__attrs__: value = getattr(self, attr) if attr.startswith("_"): attr = attr[1:] if attr != "attr_text" and value is not None: yield attr, safe_string(value)
def _explicit_none(tagname, value, namespace=None): """ Override serialisation because explicit none required """ if namespace is not None: tagname = "{%s}%s" % (namespace, tagname) return Element(tagname, val=safe_string(value))
def to_tree(self, tagname=None, value=None, namespace=None): namespace = getattr(self, "namespace", namespace) if value is not None: if namespace is not None: tagname = "{%s}%s" % (namespace, tagname) value = safe_string(value) return Element(tagname, {self.attribute: value})
def vba_code(self): for attr in ("codeName", "enableFormatConditionsCalculation", "filterMode", "published", "syncHorizontal", "syncRef", "syncVertical", "transitionEvaluation", "transitionEntry"): value = getattr(self.sheet_properties, attr) if value is not None: yield attr, safe_string(value)
def to_tree(self, tagname=None, value=None, namespace=None): namespace = getattr(self, "namespace", namespace) if value is not None: if namespace is not None: tagname = "{%s}%s" % (namespace, tagname) el = Element(tagname) el.text = safe_string(value) return el
def __iter__(self): for key in self.__attrs__: if key == "attr_text": continue v = getattr(self, key) if v is not None: if v in RESERVED: v = "_xlnm." + v yield key, safe_string(v)
def to_tree(self, tagname, obj, namespace=None): """ Convert the sequence represented by the descriptor to an XML element """ for idx, v in enumerate(obj, self.idx_base): if hasattr(v, "to_tree"): el = v.to_tree(tagname, idx) else: tagname = namespaced(obj, tagname, namespace) el = Element(tagname) el.text = safe_string(v) yield el
def to_tree(self, tagname=None, idx=None, namespace=None): if tagname is None: tagname = self.tagname # keywords have to be masked if tagname.startswith("_"): tagname = tagname[1:] tagname = namespaced(self, tagname, namespace) namespace = getattr(self, "namespace", namespace) attrs = dict(self) for key, ns in self.__namespaced__: if key in attrs: attrs[ns] = attrs[key] del attrs[key] el = Element(tagname, attrs) if "attr_text" in self.__attrs__: el.text = safe_string(getattr(self, "attr_text")) for child_tag in self.__elements__: desc = getattr(self.__class__, child_tag, None) obj = getattr(self, child_tag) if isinstance(obj, seq_types): if isinstance(desc, NestedSequence): # wrap sequence in container if not obj: continue nodes = [desc.to_tree(child_tag, obj, namespace)] elif isinstance(desc, Sequence): # sequence desc.idx_base = self.idx_base nodes = (desc.to_tree(child_tag, obj, namespace)) else: # property nodes = (v.to_tree(child_tag, namespace) for v in obj) for node in nodes: el.append(node) else: if child_tag in self.__nested__: node = desc.to_tree(child_tag, obj, namespace) elif obj is None: continue else: node = obj.to_tree(child_tag) if node is not None: el.append(node) return el
def etree_write_cell(xf, worksheet, cell, styled=None): coordinate = cell.coordinate attributes = {'r': coordinate} if styled: attributes['s'] = '%d' % cell.style_id if cell.data_type != 'f': attributes['t'] = cell.data_type value = cell._value if cell.data_type == "d": if cell.parent.parent.iso_dates: if isinstance(value, timedelta): value = days_to_time(value) value = value.isoformat() else: attributes['t'] = "n" value = to_excel(value) if cell._comment is not None: comment = CommentRecord.from_cell(cell) worksheet._comments.append(comment) el = Element("c", attributes) if value is None or value == "": xf.write(el) return if cell.data_type == 'f': shared_formula = worksheet.formula_attributes.get(coordinate, {}) formula = SubElement(el, 'f', shared_formula) if value is not None: formula.text = value[1:] value = None if cell.data_type == 's': value = worksheet.parent.shared_strings.add(value) cell_content = SubElement(el, 'v') if value is not None: cell_content.text = safe_string(value) if cell.hyperlink: worksheet._hyperlinks.append(cell.hyperlink) xf.write(el)
def lxml_write_cell(xf, worksheet, cell, styled=False): coordinate = cell.coordinate attributes = {'r': coordinate} if styled: attributes['s'] = '%d' % cell.style_id if cell.data_type != 'f': attributes['t'] = cell.data_type value = cell._value if cell.data_type == "d": if cell.parent.parent.iso_dates: if isinstance(value, timedelta): value = days_to_time(value) value = value.isoformat() else: attributes['t'] = "n" value = to_excel(value) if cell._comment is not None: comment = CommentRecord.from_cell(cell) worksheet._comments.append(comment) if value == '' or value is None: with xf.element("c", attributes): return with xf.element('c', attributes): if cell.data_type == 'f': shared_formula = worksheet.formula_attributes.get(coordinate, {}) with xf.element('f', shared_formula): if value is not None: xf.write(value[1:]) value = None if cell.data_type == 's': value = worksheet.parent.shared_strings.add(value) with xf.element("v"): if value is not None: xf.write(safe_string(value)) if cell.hyperlink: worksheet._hyperlinks.append(cell.hyperlink)
def __iter__(self): for key in self.__fields__: value = getattr(self, key, None) if value: yield key, safe_string(value)
def __iter__(self): attrs = [(self.type, self.value)] if self.tint != 0: attrs.append(('tint', self.tint)) for k, v in attrs: yield k, safe_string(v)
def to_tree(self, tagname, obj, namespace=None): tagname = namespaced(self, tagname, namespace) for v in obj: yield Element(tagname, {self.attribute: safe_string(v)})
def __iter__(self): for attr in self.__attrs__: value = getattr(self, attr) if value is not None and value != 0: yield attr, safe_string(value)
def _no_value(tagname, value, namespace=None): if value: return Element(tagname, val=safe_string(value))
def test_safe_string(value, result): from openpyxl25.compat import safe_string assert safe_string(value) == result v = safe_string('s') assert v == 's'
def test_numpy_tostring(): from numpy import float_, int_, bool_ from openpyxl25.compat.strings import safe_string assert safe_string(float_(5.1)) == "5.1" assert safe_string(int(5)) == "5" assert safe_string(bool_(True)) == "1"
def __iter__(self): for attr in self.__attrs__: value = getattr(self, attr) if value: yield attr, safe_string(value)