_writer.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from __future__ import absolute_import
  2. # Copyright (c) 2010-2019 openpyxl
  3. from openpyxl.compat import safe_string
  4. from openpyxl.xml.functions import Element, SubElement, whitespace, XML_NS, REL_NS
  5. from openpyxl import LXML
  6. from openpyxl.utils.datetime import to_excel, days_to_time
  7. from datetime import timedelta
  8. def _set_attributes(cell, styled=None):
  9. """
  10. Set coordinate and datatype
  11. """
  12. coordinate = cell.coordinate
  13. attrs = {'r': coordinate}
  14. if styled:
  15. attrs['s'] = '%d' % cell.style_id
  16. if cell.data_type == "s":
  17. attrs['t'] = "inlineStr"
  18. elif cell.data_type != 'f':
  19. attrs['t'] = cell.data_type
  20. value = cell._value
  21. if cell.data_type == "d":
  22. if cell.parent.parent.iso_dates:
  23. if isinstance(value, timedelta):
  24. value = days_to_time(value)
  25. value = value.isoformat()
  26. else:
  27. attrs['t'] = "n"
  28. value = to_excel(value, cell.parent.parent.epoch)
  29. if cell.hyperlink:
  30. cell.parent._hyperlinks.append(cell.hyperlink)
  31. return value, attrs
  32. def etree_write_cell(xf, worksheet, cell, styled=None):
  33. value, attributes = _set_attributes(cell, styled)
  34. el = Element("c", attributes)
  35. if value is None or value == "":
  36. xf.write(el)
  37. return
  38. if cell.data_type == 'f':
  39. shared_formula = worksheet.formula_attributes.get(cell.coordinate, {})
  40. formula = SubElement(el, 'f', shared_formula)
  41. if value is not None:
  42. formula.text = value[1:]
  43. value = None
  44. if cell.data_type == 's':
  45. inline_string = SubElement(el, 'is')
  46. text = SubElement(inline_string, 't')
  47. text.text = value
  48. whitespace(text)
  49. else:
  50. cell_content = SubElement(el, 'v')
  51. if value is not None:
  52. cell_content.text = safe_string(value)
  53. xf.write(el)
  54. def lxml_write_cell(xf, worksheet, cell, styled=False):
  55. value, attributes = _set_attributes(cell, styled)
  56. if value == '' or value is None:
  57. with xf.element("c", attributes):
  58. return
  59. with xf.element('c', attributes):
  60. if cell.data_type == 'f':
  61. shared_formula = worksheet.formula_attributes.get(cell.coordinate, {})
  62. with xf.element('f', shared_formula):
  63. if value is not None:
  64. xf.write(value[1:])
  65. value = None
  66. if cell.data_type == 's':
  67. with xf.element("is"):
  68. attrs = {}
  69. if value != value.strip():
  70. attrs["{%s}space" % XML_NS] = "preserve"
  71. el = Element("t", attrs) # lxml can't handle xml-ns
  72. el.text = value
  73. xf.write(el)
  74. #with xf.element("t", attrs):
  75. #xf.write(value)
  76. else:
  77. with xf.element("v"):
  78. if value is not None:
  79. xf.write(safe_string(value))
  80. if LXML:
  81. write_cell = lxml_write_cell
  82. else:
  83. write_cell = etree_write_cell