core.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from __future__ import absolute_import
  2. # Copyright (c) 2010-2019 openpyxl
  3. import datetime
  4. from openpyxl.compat import safe_string, unicode
  5. from openpyxl.utils.datetime import (
  6. CALENDAR_WINDOWS_1900,
  7. to_ISO8601,
  8. from_ISO8601,
  9. )
  10. from openpyxl.descriptors import (
  11. String,
  12. DateTime,
  13. Alias,
  14. )
  15. from openpyxl.descriptors.serialisable import Serialisable
  16. from openpyxl.descriptors.nested import NestedText
  17. from openpyxl.xml.functions import (Element, QName, tostring)
  18. from openpyxl.xml.constants import (
  19. COREPROPS_NS,
  20. DCORE_NS,
  21. XSI_NS,
  22. DCTERMS_NS,
  23. DCTERMS_PREFIX
  24. )
  25. class NestedDateTime(DateTime, NestedText):
  26. expected_type = datetime.datetime
  27. def to_tree(self, tagname=None, value=None, namespace=None):
  28. namespace = getattr(self, "namespace", namespace)
  29. if namespace is not None:
  30. tagname = "{%s}%s" % (namespace, tagname)
  31. el = Element(tagname)
  32. if value is not None:
  33. el.text = to_ISO8601(value)
  34. return el
  35. class QualifiedDateTime(NestedDateTime):
  36. """In certain situations Excel will complain if the additional type
  37. attribute isn't set"""
  38. def to_tree(self, tagname=None, value=None, namespace=None):
  39. el = super(QualifiedDateTime, self).to_tree(tagname, value, namespace)
  40. el.set("{%s}type" % XSI_NS, QName(DCTERMS_NS, "W3CDTF"))
  41. return el
  42. class DocumentProperties(Serialisable):
  43. """High-level properties of the document.
  44. Defined in ECMA-376 Par2 Annex D
  45. """
  46. tagname = "coreProperties"
  47. namespace = COREPROPS_NS
  48. category = NestedText(expected_type=unicode, allow_none=True)
  49. contentStatus = NestedText(expected_type=unicode, allow_none=True)
  50. keywords = NestedText(expected_type=unicode, allow_none=True)
  51. lastModifiedBy = NestedText(expected_type=unicode, allow_none=True)
  52. lastPrinted = NestedDateTime(allow_none=True)
  53. revision = NestedText(expected_type=unicode, allow_none=True)
  54. version = NestedText(expected_type=unicode, allow_none=True)
  55. last_modified_by = Alias("lastModifiedBy")
  56. # Dublin Core Properties
  57. subject = NestedText(expected_type=unicode, allow_none=True, namespace=DCORE_NS)
  58. title = NestedText(expected_type=unicode, allow_none=True, namespace=DCORE_NS)
  59. creator = NestedText(expected_type=unicode, allow_none=True, namespace=DCORE_NS)
  60. description = NestedText(expected_type=unicode, allow_none=True, namespace=DCORE_NS)
  61. identifier = NestedText(expected_type=unicode, allow_none=True, namespace=DCORE_NS)
  62. language = NestedText(expected_type=unicode, allow_none=True, namespace=DCORE_NS)
  63. # Dublin Core Terms
  64. created = QualifiedDateTime(allow_none=True, namespace=DCTERMS_NS)
  65. modified = QualifiedDateTime(allow_none=True, namespace=DCTERMS_NS)
  66. __elements__ = ("creator", "title", "description", "subject","identifier",
  67. "language", "created", "modified", "lastModifiedBy", "category",
  68. "contentStatus", "version", "revision", "keywords", "lastPrinted",
  69. )
  70. def __init__(self,
  71. category=None,
  72. contentStatus=None,
  73. keywords=None,
  74. lastModifiedBy=None,
  75. lastPrinted=None,
  76. revision=None,
  77. version=None,
  78. created=datetime.datetime.now(),
  79. creator="openpyxl",
  80. description=None,
  81. identifier=None,
  82. language=None,
  83. modified=datetime.datetime.now(),
  84. subject=None,
  85. title=None,
  86. ):
  87. self.contentStatus = contentStatus
  88. self.lastPrinted = lastPrinted
  89. self.revision = revision
  90. self.version = version
  91. self.creator = creator
  92. self.lastModifiedBy = lastModifiedBy
  93. self.modified = modified
  94. self.created = created
  95. self.title = title
  96. self.subject = subject
  97. self.description = description
  98. self.identifier = identifier
  99. self.language = language
  100. self.keywords = keywords
  101. self.category = category