excel.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Copyright (c) 2010-2024 openpyxl
  2. """
  3. Excel specific descriptors
  4. """
  5. from openpyxl.xml.constants import REL_NS
  6. from openpyxl.compat import safe_string
  7. from openpyxl.xml.functions import Element
  8. from . import (
  9. MatchPattern,
  10. MinMax,
  11. Integer,
  12. String,
  13. Sequence,
  14. )
  15. from .serialisable import Serialisable
  16. class HexBinary(MatchPattern):
  17. pattern = "[0-9a-fA-F]+$"
  18. class UniversalMeasure(MatchPattern):
  19. pattern = r"[0-9]+(\.[0-9]+)?(mm|cm|in|pt|pc|pi)"
  20. class TextPoint(MinMax):
  21. """
  22. Size in hundredths of points.
  23. In theory other units of measurement can be used but these are unbounded
  24. """
  25. expected_type = int
  26. min = -400000
  27. max = 400000
  28. Coordinate = Integer
  29. class Percentage(MinMax):
  30. pattern = r"((100)|([0-9][0-9]?))(\.[0-9][0-9]?)?%" # strict
  31. min = -1000000
  32. max = 1000000
  33. def __set__(self, instance, value):
  34. if isinstance(value, str) and "%" in value:
  35. value = value.replace("%", "")
  36. value = int(float(value) * 1000)
  37. super().__set__(instance, value)
  38. class Extension(Serialisable):
  39. uri = String()
  40. def __init__(self,
  41. uri=None,
  42. ):
  43. self.uri = uri
  44. class ExtensionList(Serialisable):
  45. ext = Sequence(expected_type=Extension)
  46. def __init__(self,
  47. ext=(),
  48. ):
  49. self.ext = ext
  50. class Relation(String):
  51. namespace = REL_NS
  52. allow_none = True
  53. class Base64Binary(MatchPattern):
  54. # http://www.w3.org/TR/xmlschema11-2/#nt-Base64Binary
  55. pattern = "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$"
  56. class Guid(MatchPattern):
  57. # https://msdn.microsoft.com/en-us/library/dd946381(v=office.12).aspx
  58. pattern = r"{[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\}"
  59. class CellRange(MatchPattern):
  60. pattern = r"^[$]?([A-Za-z]{1,3})[$]?(\d+)(:[$]?([A-Za-z]{1,3})[$]?(\d+)?)?$|^[A-Za-z]{1,3}:[A-Za-z]{1,3}$"
  61. allow_none = True
  62. def __set__(self, instance, value):
  63. if value is not None:
  64. value = value.upper()
  65. super().__set__(instance, value)
  66. def _explicit_none(tagname, value, namespace=None):
  67. """
  68. Override serialisation because explicit none required
  69. """
  70. if namespace is not None:
  71. tagname = "{%s}%s" % (namespace, tagname)
  72. return Element(tagname, val=safe_string(value))