Skip to content

Commit 1eff802

Browse files
committed
add alignment support
1 parent f7168bc commit 1eff802

6 files changed

Lines changed: 104 additions & 13 deletions

File tree

‎pyexcelerate/Alignment.py‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from . import six
2+
from . import Utility
3+
from . import Color
4+
5+
class Alignment(object):
6+
def __init__(self, horizontal='left', vertical='bottom', rotation=0):
7+
self._horizontal = horizontal
8+
self._vertical = vertical
9+
self._rotation = rotation
10+
11+
@property
12+
def horizontal(self):
13+
return self._horizontal
14+
15+
@horizontal.setter
16+
def horizontal(self, value):
17+
if value not in ('left', 'center', 'right'):
18+
raise Exception('Invalid horizontal alignment value.')
19+
self._horizontal = value
20+
21+
@property
22+
def vertical(self):
23+
return self._vertical
24+
25+
@vertical.setter
26+
def vertical(self, value):
27+
if value not in ('top', 'center', 'bottom'):
28+
raise Exception('Invalid vertical alignment value.')
29+
self._vertical = value
30+
31+
@property
32+
def rotation(self):
33+
return self._rotation
34+
35+
@rotation.setter
36+
def rotation(self, value):
37+
self._rotation = (value % 360)
38+
39+
@property
40+
def is_default(self):
41+
return self._horizontal == 'left' and self._vertical == 'bottom' and self._rotation == 0
42+
43+
def get_xml_string(self):
44+
return "<alignment horizontal=\"%s\" vertical=\"%s\" textRotation=\"%.15g\"/>" % (self._horizontal, self._vertical, self._rotation)
45+
46+
def __or__(self, other):
47+
return self._binary_operation(other, Utility.nonboolean_or)
48+
49+
def __and__(self, other):
50+
return self._binary_operation(other, Utility.nonboolean_and)
51+
52+
def __xor__(self, other):
53+
return self._binary_operation(other, Utility.nonboolean_xor)
54+
55+
def _binary_operation(self, other, operation):
56+
return Alignment( \
57+
horizontal = operation(self._horizontal, other._horizontal, 'left'), \
58+
vertical = operation(self._vertical, other._vertical, 'bottom'), \
59+
rotation = operation(self._rotation, other._rotation, 0) \
60+
)
61+
62+
def __eq__(self, other):
63+
if other is None:
64+
return self.is_default
65+
elif Utility.YOLO:
66+
return self._vertical == other._vertical and self._rotation == other._rotation
67+
else:
68+
return self._vertical == other._vertical and self._rotation == other._rotation and self._horizontal == other._horizontal
69+
70+
def __hash__(self):
71+
return hash((self._horizontal))
72+
73+
def __str__(self):
74+
return "Align: %s %s %s" % (self._horizontal, self._vertical, self._rotation)
75+

‎pyexcelerate/Fill.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def background(self):
1111

1212
@background.setter
1313
def background(self, value):
14-
Utility.lazy_set(self, '_background', Color.Color(), value)
14+
Utility.lazy_set(self, '_background', None, value)
1515

1616
@property
1717
def is_default(self):

‎pyexcelerate/Font.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def color(self):
3333

3434
@color.setter
3535
def color(self, value):
36-
Utility.lazy_set(self, '_color', Color.Color(), value)
36+
Utility.lazy_set(self, '_color', None, value)
3737

3838
@property
3939
def is_default(self):

‎pyexcelerate/Style.py‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,68 @@
11
from . import six
2-
from . import Font, Fill, Format
2+
from . import Font, Fill, Format, Alignment
33
from . import Utility
44

55
class Style(object):
66
_DEFAULT_FORMAT = Format.Format()
77
_DEFAULT_FILL = Fill.Fill()
88
_DEFAULT_FONT = Font.Font()
9-
def __init__(self, font=None, fill=None, format=None):
9+
def __init__(self, font=None, fill=None, format=None, alignment=None):
1010
self._font = font
1111
self._fill = fill
1212
self._format = format
13+
self._alignment = alignment
1314

1415
@property
1516
def is_default(self):
16-
return not (self._font or self._fill or self._format)
17+
return not (self._font or self._fill or self._format or self._alignment)
1718

19+
@property
20+
def alignment(self):
21+
return Utility.lazy_get(self, '_alignment', Alignment.Alignment())
22+
23+
@alignment.setter
24+
def alignment(self, value):
25+
Utility.lazy_set(self, '_alignment', None, value)
26+
1827
@property
1928
def format(self):
2029
# don't use default because default should be const
2130
return Utility.lazy_get(self, '_format', Format.Format())
2231

2332
@format.setter
2433
def format(self, value):
25-
Utility.lazy_set(self, '_format', Style._DEFAULT_FORMAT, value)
34+
Utility.lazy_set(self, '_format', None, value)
2635

2736
@property
2837
def font(self):
2938
return Utility.lazy_get(self, '_font', Font.Font())
3039

3140
@font.setter
3241
def font(self, value):
33-
Utility.lazy_set(self, '_font', Style._DEFAULT_FONT, value)
42+
Utility.lazy_set(self, '_font', None, value)
3443

3544
@property
3645
def fill(self):
3746
return Utility.lazy_get(self, '_fill', Fill.Fill())
3847

3948
@fill.setter
4049
def fill(self, value):
41-
Utility.lazy_set(self, '_fill', Style._DEFAULT_FILL, value)
50+
Utility.lazy_set(self, '_fill', None, value)
4251

4352
def get_xml_string(self):
4453
# Precondition: Workbook._align_styles has been run.
4554
# Be careful when using this function as id's may be inaccurate if precondition not met.
4655
tag = []
47-
if not self.format.is_default:
56+
if not self._format is None:
4857
tag.append("numFmtId=\"%d\"" % self.format.id)
49-
if not self.font.is_default:
58+
if not self._font is None:
5059
tag.append("applyFont=\"1\" fontId=\"%d\"" % (self.font.id))
51-
if not self.fill.is_default:
60+
if not self._fill is None:
5261
tag.append("applyFill=\"1\" fillId=\"%d\"" % (self.fill.id + 1))
53-
return "<xf xfId=\"0\" borderId=\"0\" %s/>" % (" ".join(tag))
62+
if self._alignment is None:
63+
return "<xf xfId=\"0\" borderId=\"0\" %s/>" % (" ".join(tag))
64+
else:
65+
return "<xf xfId=\"0\" borderId=\"0\" %s applyAlignment=\"1\">%s</xf>" % (" ".join(tag), self._alignment.get_xml_string())
5466

5567
def __hash__(self):
5668
return hash(self._font)

‎pyexcelerate/Utility.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def lazy_get(self, attribute, default):
3333

3434
def lazy_set(self, attribute, default, value):
3535
if value == default:
36-
setattr(self, attribute, None)
36+
setattr(self, attribute, default)
3737
else:
3838
setattr(self, attribute, value)
39+
3940
YOLO = False # are we aligning?

‎pyexcelerate/tests/test_Style.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ def test_style():
2727
ws[2][2].value = "qwer"
2828
eq_(ws[1][2].value, ws[1][1].value)
2929
ws[2][1].style.fill.background = Color(0, 255, 0, 0)
30+
ws[1][1].style.alignment.vertical = 'top'
31+
ws[1][1].style.alignment.horizontal = 'right'
32+
ws[1][1].style.alignment.rotation = 90
3033
wb.save(get_output_path("style-test.xlsx"))
3134

3235
def test_style_compression():

0 commit comments

Comments
 (0)