1
2# Copyright (c) 2010-2024 openpyxl
3
4import math
5
6from openpyxl.utils.units import pixels_to_EMU
7
8
9class Drawing:
10 """ a drawing object - eg container for shapes or charts
11 we assume user specifies dimensions in pixels; units are
12 converted to EMU in the drawing part
13 """
14
15 count = 0
16
17 def __init__(self):
18
19 self.name = ''
20 self.description = ''
21 self.coordinates = ((1, 2), (16, 8))
22 self.left = 0
23 self.top = 0
24 self._width = 21 # default in px
25 self._height = 192 #default in px
26 self.resize_proportional = False
27 self.rotation = 0
28 self.anchortype = "absolute"
29 self.anchorcol = 0 # left cell
30 self.anchorrow = 0 # top row
31
32
33 @property
34 def width(self):
35 return self._width
36
37
38 @width.setter
39 def width(self, w):
40 if self.resize_proportional and w:
41 ratio = self._height / self._width
42 self._height = round(ratio * w)
43 self._width = w
44
45
46 @property
47 def height(self):
48 return self._height
49
50
51 @height.setter
52 def height(self, h):
53 if self.resize_proportional and h:
54 ratio = self._width / self._height
55 self._width = round(ratio * h)
56 self._height = h
57
58
59 def set_dimension(self, w=0, h=0):
60
61 xratio = w / self._width
62 yratio = h / self._height
63
64 if self.resize_proportional and w and h:
65 if (xratio * self._height) < h:
66 self._height = math.ceil(xratio * self._height)
67 self._width = w
68 else:
69 self._width = math.ceil(yratio * self._width)
70 self._height = h
71
72
73 @property
74 def anchor(self):
75 from .spreadsheet_drawing import (
76 OneCellAnchor,
77 TwoCellAnchor,
78 AbsoluteAnchor)
79 if self.anchortype == "absolute":
80 anchor = AbsoluteAnchor()
81 anchor.pos.x = pixels_to_EMU(self.left)
82 anchor.pos.y = pixels_to_EMU(self.top)
83
84 elif self.anchortype == "oneCell":
85 anchor = OneCellAnchor()
86 anchor._from.col = self.anchorcol
87 anchor._from.row = self.anchorrow
88
89 anchor.ext.width = pixels_to_EMU(self._width)
90 anchor.ext.height = pixels_to_EMU(self._height)
91
92 return anchor