fill.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. from __future__ import absolute_import
  2. # Copyright (c) 2010-2019 openpyxl
  3. from openpyxl.compat import unicode
  4. from openpyxl.descriptors.serialisable import Serialisable
  5. from openpyxl.descriptors import (
  6. Alias,
  7. Bool,
  8. Integer,
  9. Set,
  10. NoneSet,
  11. Typed,
  12. MinMax,
  13. Sequence,
  14. )
  15. from openpyxl.descriptors.excel import (
  16. Relation,
  17. Percentage,
  18. )
  19. from openpyxl.descriptors.nested import NestedNoneSet, NestedValue
  20. from openpyxl.descriptors.sequence import NestedSequence
  21. from openpyxl.xml.constants import DRAWING_NS
  22. from .colors import (
  23. ColorChoice,
  24. HSLColor,
  25. SystemColor,
  26. SchemeColor,
  27. RGBPercent,
  28. PRESET_COLORS,
  29. )
  30. from openpyxl.descriptors.excel import ExtensionList as OfficeArtExtensionList
  31. from .effect import *
  32. """
  33. Fill elements from drawing main schema
  34. """
  35. class PatternFillProperties(Serialisable):
  36. tagname = "pattFill"
  37. namespace = DRAWING_NS
  38. prst = NoneSet(values=(['pct5', 'pct10', 'pct20', 'pct25', 'pct30', 'pct40',
  39. 'pct50', 'pct60', 'pct70', 'pct75', 'pct80', 'pct90', 'horz', 'vert',
  40. 'ltHorz', 'ltVert', 'dkHorz', 'dkVert', 'narHorz', 'narVert', 'dashHorz',
  41. 'dashVert', 'cross', 'dnDiag', 'upDiag', 'ltDnDiag', 'ltUpDiag',
  42. 'dkDnDiag', 'dkUpDiag', 'wdDnDiag', 'wdUpDiag', 'dashDnDiag',
  43. 'dashUpDiag', 'diagCross', 'smCheck', 'lgCheck', 'smGrid', 'lgGrid',
  44. 'dotGrid', 'smConfetti', 'lgConfetti', 'horzBrick', 'diagBrick',
  45. 'solidDmnd', 'openDmnd', 'dotDmnd', 'plaid', 'sphere', 'weave', 'divot',
  46. 'shingle', 'wave', 'trellis', 'zigZag']))
  47. preset = Alias("prst")
  48. fgClr = Typed(expected_type=ColorChoice, allow_none=True)
  49. foreground = Alias("fgClr")
  50. bgClr = Typed(expected_type=ColorChoice, allow_none=True)
  51. background = Alias("bgClr")
  52. __elements__ = ("fgClr", "bgClr")
  53. def __init__(self,
  54. prst=None,
  55. fgClr=None,
  56. bgClr=None,
  57. ):
  58. self.prst = prst
  59. self.fgClr = fgClr
  60. self.bgClr = bgClr
  61. class RelativeRect(Serialisable):
  62. tagname = "rect"
  63. namespace = DRAWING_NS
  64. l = Percentage(allow_none=True)
  65. left = Alias('l')
  66. t = Percentage(allow_none=True)
  67. top = Alias('t')
  68. r = Percentage(allow_none=True)
  69. right = Alias('r')
  70. b = Percentage(allow_none=True)
  71. bottom = Alias('b')
  72. def __init__(self,
  73. l=None,
  74. t=None,
  75. r=None,
  76. b=None,
  77. ):
  78. self.l = l
  79. self.t = t
  80. self.r = r
  81. self.b = b
  82. class StretchInfoProperties(Serialisable):
  83. tagname = "stretch"
  84. namespace = DRAWING_NS
  85. fillRect = Typed(expected_type=RelativeRect, allow_none=True)
  86. def __init__(self,
  87. fillRect=RelativeRect(),
  88. ):
  89. self.fillRect = fillRect
  90. class GradientStop(Serialisable):
  91. tagname = "gs"
  92. namespace = DRAWING_NS
  93. pos = MinMax(min=0, max=100000, allow_none=True)
  94. # Color Choice Group
  95. scrgbClr = Typed(expected_type=RGBPercent, allow_none=True)
  96. RGBPercent = Alias('scrgbClr')
  97. srgbClr = NestedValue(expected_type=unicode, allow_none=True) # needs pattern and can have transform
  98. RGB = Alias('srgbClr')
  99. hslClr = Typed(expected_type=HSLColor, allow_none=True)
  100. sysClr = Typed(expected_type=SystemColor, allow_none=True)
  101. schemeClr = Typed(expected_type=SchemeColor, allow_none=True)
  102. prstClr = NestedNoneSet(values=PRESET_COLORS)
  103. __elements__ = ('scrgbClr', 'srgbClr', 'hslClr', 'sysClr', 'schemeClr', 'prstClr')
  104. def __init__(self,
  105. pos=None,
  106. scrgbClr=None,
  107. srgbClr=None,
  108. hslClr=None,
  109. sysClr=None,
  110. schemeClr=None,
  111. prstClr=None,
  112. ):
  113. if pos is None:
  114. pos = 0
  115. self.pos = pos
  116. self.scrgbClr = scrgbClr
  117. self.srgbClr = srgbClr
  118. self.hslClr = hslClr
  119. self.sysClr = sysClr
  120. self.schemeClr = schemeClr
  121. self.prstClr = prstClr
  122. class LinearShadeProperties(Serialisable):
  123. tagname = "lin"
  124. namespace = DRAWING_NS
  125. ang = Integer()
  126. scaled = Bool(allow_none=True)
  127. def __init__(self,
  128. ang=None,
  129. scaled=None,
  130. ):
  131. self.ang = ang
  132. self.scaled = scaled
  133. class PathShadeProperties(Serialisable):
  134. tagname = "path"
  135. namespace = DRAWING_NS
  136. path = Set(values=(['shape', 'circle', 'rect']))
  137. fillToRect = Typed(expected_type=RelativeRect, allow_none=True)
  138. def __init__(self,
  139. path=None,
  140. fillToRect=None,
  141. ):
  142. self.path = path
  143. self.fillToRect = fillToRect
  144. class GradientFillProperties(Serialisable):
  145. tagname = "gradFill"
  146. namespace = DRAWING_NS
  147. flip = NoneSet(values=(['x', 'y', 'xy']))
  148. rotWithShape = Bool(allow_none=True)
  149. gsLst = NestedSequence(expected_type=GradientStop, count=False)
  150. stop_list = Alias("gsLst")
  151. lin = Typed(expected_type=LinearShadeProperties, allow_none=True)
  152. linear = Alias("lin")
  153. path = Typed(expected_type=PathShadeProperties, allow_none=True)
  154. tileRect = Typed(expected_type=RelativeRect, allow_none=True)
  155. __elements__ = ('gsLst', 'lin', 'path', 'tileRect')
  156. def __init__(self,
  157. flip=None,
  158. rotWithShape=None,
  159. gsLst=(),
  160. lin=None,
  161. path=None,
  162. tileRect=None,
  163. ):
  164. self.flip = flip
  165. self.rotWithShape = rotWithShape
  166. self.gsLst = gsLst
  167. self.lin = lin
  168. self.path = path
  169. self.tileRect = tileRect
  170. class SolidColorFillProperties(Serialisable):
  171. tagname = "solidFill"
  172. # uses element group EG_ColorChoice
  173. scrgbClr = Typed(expected_type=RGBPercent, allow_none=True)
  174. RGBPercent = Alias('scrgbClr')
  175. srgbClr = NestedValue(expected_type=unicode, allow_none=True) # needs pattern and can have transform
  176. RGB = Alias('srgbClr')
  177. hslClr = Typed(expected_type=HSLColor, allow_none=True)
  178. sysClr = Typed(expected_type=SystemColor, allow_none=True)
  179. schemeClr = Typed(expected_type=SchemeColor, allow_none=True)
  180. prstClr = NestedNoneSet(values=PRESET_COLORS)
  181. __elements__ = ('scrgbClr', 'srgbClr', 'hslClr', 'sysClr', 'schemeClr', 'prstClr')
  182. def __init__(self,
  183. scrgbClr=None,
  184. srgbClr=None,
  185. hslClr=None,
  186. sysClr=None,
  187. schemeClr=None,
  188. prstClr=None,
  189. ):
  190. self.scrgbClr = scrgbClr
  191. self.srgbClr = srgbClr
  192. self.hslClr = hslClr
  193. self.sysClr = sysClr
  194. self.schemeClr = schemeClr
  195. self.prstClr = prstClr
  196. class Blip(Serialisable):
  197. tagname = "blip"
  198. namespace = DRAWING_NS
  199. #Using attribute groupAG_Blob
  200. cstate = NoneSet(values=(['email', 'screen', 'print', 'hqprint']))
  201. embed = Relation() #rId
  202. link = Relation() #hyperlink
  203. noGrp = Bool(allow_none=True)
  204. noSelect = Bool(allow_none=True)
  205. noRot = Bool(allow_none=True)
  206. noChangeAspect = Bool(allow_none=True)
  207. noMove = Bool(allow_none=True)
  208. noResize = Bool(allow_none=True)
  209. noEditPoints = Bool(allow_none=True)
  210. noAdjustHandles = Bool(allow_none=True)
  211. noChangeArrowheads = Bool(allow_none=True)
  212. noChangeShapeType = Bool(allow_none=True)
  213. # some elements are choice
  214. extLst = Typed(expected_type=OfficeArtExtensionList, allow_none=True)
  215. alphaBiLevel = Typed(expected_type=AlphaBiLevelEffect, allow_none=True)
  216. alphaCeiling = Typed(expected_type=AlphaCeilingEffect, allow_none=True)
  217. alphaFloor = Typed(expected_type=AlphaFloorEffect, allow_none=True)
  218. alphaInv = Typed(expected_type=AlphaInverseEffect, allow_none=True)
  219. alphaMod = Typed(expected_type=AlphaModulateEffect, allow_none=True)
  220. alphaModFix = Typed(expected_type=AlphaModulateFixedEffect, allow_none=True)
  221. alphaRepl = Typed(expected_type=AlphaReplaceEffect, allow_none=True)
  222. biLevel = Typed(expected_type=BiLevelEffect, allow_none=True)
  223. blur = Typed(expected_type=BlurEffect, allow_none=True)
  224. clrChange = Typed(expected_type=ColorChangeEffect, allow_none=True)
  225. clrRepl = Typed(expected_type=ColorReplaceEffect, allow_none=True)
  226. duotone = Typed(expected_type=DuotoneEffect, allow_none=True)
  227. fillOverlay = Typed(expected_type=FillOverlayEffect, allow_none=True)
  228. grayscl = Typed(expected_type=GrayscaleEffect, allow_none=True)
  229. hsl = Typed(expected_type=HSLEffect, allow_none=True)
  230. lum = Typed(expected_type=LuminanceEffect, allow_none=True)
  231. tint = Typed(expected_type=TintEffect, allow_none=True)
  232. __elements__ = ('alphaBiLevel', 'alphaCeiling', 'alphaFloor', 'alphaInv',
  233. 'alphaMod', 'alphaModFix', 'alphaRepl', 'biLevel', 'blur', 'clrChange',
  234. 'clrRepl', 'duotone', 'fillOverlay', 'grayscl', 'hsl', 'lum', 'tint')
  235. def __init__(self,
  236. cstate=None,
  237. embed=None,
  238. link=None,
  239. noGrp=None,
  240. noSelect=None,
  241. noRot=None,
  242. noChangeAspect=None,
  243. noMove=None,
  244. noResize=None,
  245. noEditPoints=None,
  246. noAdjustHandles=None,
  247. noChangeArrowheads=None,
  248. noChangeShapeType=None,
  249. extLst=None,
  250. alphaBiLevel=None,
  251. alphaCeiling=None,
  252. alphaFloor=None,
  253. alphaInv=None,
  254. alphaMod=None,
  255. alphaModFix=None,
  256. alphaRepl=None,
  257. biLevel=None,
  258. blur=None,
  259. clrChange=None,
  260. clrRepl=None,
  261. duotone=None,
  262. fillOverlay=None,
  263. grayscl=None,
  264. hsl=None,
  265. lum=None,
  266. tint=None,
  267. ):
  268. self.cstate = cstate
  269. self.embed = embed
  270. self.link = link
  271. self.noGrp = noGrp
  272. self.noSelect = noSelect
  273. self.noRot = noRot
  274. self.noChangeAspect = noChangeAspect
  275. self.noMove = noMove
  276. self.noResize = noResize
  277. self.noEditPoints = noEditPoints
  278. self.noAdjustHandles = noAdjustHandles
  279. self.noChangeArrowheads = noChangeArrowheads
  280. self.noChangeShapeType = noChangeShapeType
  281. self.extLst = extLst
  282. self.alphaBiLevel = alphaBiLevel
  283. self.alphaCeiling = alphaCeiling
  284. self.alphaFloor = alphaFloor
  285. self.alphaInv = alphaInv
  286. self.alphaMod = alphaMod
  287. self.alphaModFix = alphaModFix
  288. self.alphaRepl = alphaRepl
  289. self.biLevel = biLevel
  290. self.blur = blur
  291. self.clrChange = clrChange
  292. self.clrRepl = clrRepl
  293. self.duotone = duotone
  294. self.fillOverlay = fillOverlay
  295. self.grayscl = grayscl
  296. self.hsl = hsl
  297. self.lum = lum
  298. self.tint = tint
  299. class TileInfoProperties(Serialisable):
  300. tx = Integer(allow_none=True)
  301. ty = Integer(allow_none=True)
  302. sx = Integer(allow_none=True)
  303. sy = Integer(allow_none=True)
  304. flip = NoneSet(values=(['x', 'y', 'xy']))
  305. algn = Set(values=(['tl', 't', 'tr', 'l', 'ctr', 'r', 'bl', 'b', 'br']))
  306. def __init__(self,
  307. tx=None,
  308. ty=None,
  309. sx=None,
  310. sy=None,
  311. flip=None,
  312. algn=None,
  313. ):
  314. self.tx = tx
  315. self.ty = ty
  316. self.sx = sx
  317. self.sy = sy
  318. self.flip = flip
  319. self.algn = algn
  320. class BlipFillProperties(Serialisable):
  321. tagname = "blipFill"
  322. dpi = Integer(allow_none=True)
  323. rotWithShape = Bool(allow_none=True)
  324. blip = Typed(expected_type=Blip, allow_none=True)
  325. srcRect = Typed(expected_type=RelativeRect, allow_none=True)
  326. tile = Typed(expected_type=TileInfoProperties, allow_none=True)
  327. stretch = Typed(expected_type=StretchInfoProperties, allow_none=True)
  328. __elements__ = ("blip", "srcRect", "tile", "stretch")
  329. def __init__(self,
  330. dpi=None,
  331. rotWithShape=None,
  332. blip=None,
  333. tile=None,
  334. stretch=StretchInfoProperties(),
  335. srcRect=None,
  336. ):
  337. self.dpi = dpi
  338. self.rotWithShape = rotWithShape
  339. self.blip = blip
  340. self.tile = tile
  341. self.stretch = stretch
  342. self.srcRect = srcRect