forked from kz26/PyExcelerate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_Style.py
More file actions
155 lines (139 loc) · 6.05 KB
/
test_Style.py
File metadata and controls
155 lines (139 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# -*- coding: utf-8 -*-
from ..Workbook import Workbook
from ..Color import Color
from ..Font import Font
from ..Fill import Fill
from ..Style import Style
from ..Alignment import Alignment
import time
from datetime import datetime
import nose
from nose.tools import eq_, ok_, raises
from .utils import get_output_path
def test_style():
wb = Workbook()
ws = wb.new_sheet("test")
ws[1][1].value = 1
ws[1][2].value = 1
ws[1][3].value = 1
ws[1][1].style.font.bold = True
ws[1][2].style.font.italic = True
ws[1][3].style.font.underline = True
ws[1][1].style.font.strikethrough = True
ws[1][1].style.font.color = Color(255, 0, 255)
ws[1][1].style.fill.background = Color(0, 255, 0)
ws[1][2].style.fill.background = Color(255, 255, 0)
ws[2][1].value = "asdf"
ws.range("A2", "B2").merge()
eq_(ws[1][2].value, ws[1][1].value)
ws[2][2].value = "qwer"
eq_(ws[1][2].value, ws[1][1].value)
ws[2][1].style.fill.background = Color(0, 255, 0)
ws[1][1].style.alignment.vertical = 'top'
ws[1][1].style.alignment.horizontal = 'right'
ws[1][1].style.alignment.rotation = 90
eq_(ws[1][1].style.alignment.rotation, 90)
ws[3][3].style.borders.top.color = Color(255, 0, 0)
ws[3][3].style.borders.left.color = Color(0, 255, 0)
ws[3][4].style.borders.right.style = '-.'
ws[4][1].value = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla lobortis fermentum metus id congue. Sed ultrices velit id sapien sodales bibendum. Mauris volutpat porta arcu non bibendum. Pellentesque adipiscing lacus quam, ac congue ipsum fringilla sed. Praesent dapibus dignissim elit vel dictum. Pellentesque commodo iaculis ipsum a rhoncus. Sed mattis neque eget justo dignissim scelerisque. Nam odio neque, mattis et libero id, posuere aliquam mi.'
ws[4][2].value = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla lobortis fermentum metus id congue. Sed ultrices velit id sapien sodales bibendum. Mauris volutpat porta arcu non bibendum. Pellentesque adipiscing lacus quam, ac congue ipsum fringilla sed. Praesent dapibus dignissim elit vel dictum. Pellentesque commodo iaculis ipsum a rhoncus. Sed mattis neque eget justo dignissim scelerisque. Nam odio neque, mattis et libero id, posuere aliquam mi.'
ws[4][1].style.alignment.wrap_text = True
wb.save(get_output_path("style-test.xlsx"))
@raises(TypeError)
def test_invalid_wrap_text():
a = Alignment()
a.wrap_text = True
ok_(a.wrap_text)
a.wrap_text = 'some random nonsense'
@raises(ValueError)
def test_invalid_horizontal():
a = Alignment()
a.horizontal = 'left'
eq_(a.horizontal, 'left')
a.horizontal = 'nowhere'
@raises(ValueError)
def test_invalid_vertical():
a = Alignment()
a.vertical = 'bottom'
eq_(a.vertical, 'bottom')
a.vertical = 'somewhere'
def test_style_compression():
wb = Workbook()
ws = wb.new_sheet("test")
ws.range("A1","C3").value = 1
ws.range("A1","C1").style.font.bold = True
ws.range("A2","C3").style.font.italic = True
ws.range("A3","C3").style.fill.background = Color(255, 0, 0)
ws.range("C1","C3").style.font.strikethrough = True
wb.save(get_output_path("style-compression-test.xlsx"))
def test_style_reference():
wb = Workbook()
ws = wb.new_sheet("test")
ws[1][1].value = 1
font = Font(bold=True, italic=True, underline=True, strikethrough=True)
ws[1][1].style.font = font
wb.save(get_output_path("style-reference-test.xlsx"))
def test_style_row():
wb = Workbook()
ws = wb.new_sheet("test")
ws[1].style.fill.background = Color(255, 0, 0)
ws[1][3].style.fill.background = Color(0, 255, 0)
wb.save(get_output_path("style-row-test.xlsx"))
def test_style_row_col():
wb = Workbook()
ws = wb.new_sheet("test")
ws[1][1].value = 'asdf'
ws[1][3].value = 'asdf\nasdf\nasdf\nasdf'
ws[3][1].value = 'asdfasdfasdfasdfasdfasdfasdfasdf'
eq_(Style(), ws.get_row_style(1))
eq_(Style(), ws.get_col_style(1))
ws.set_row_style(1, Style(size=-1))
ws.set_row_style(2, Style(size=-1))
ws.set_row_style(3, Style(size=-1))
ws.set_row_style(4, Style(size=0))
ws.set_row_style(5, Style(size=100, fill=Fill(background=Color(0, 255, 0, 0))))
ws.set_col_style(1, Style(size=-1))
ws.set_col_style(2, Style(size=-1))
ws.set_col_style(3, Style(size=-1))
ws.set_col_style(4, Style(size=0))
ws.set_col_style(5, Style(size=100, fill=Fill(background=Color(255, 0, 0, 0))))
wb.save(get_output_path("style-auto-row-col-test.xlsx"))
def test_and_or_xor():
bolditalic = Font(bold=True, italic=True)
italicunderline = Font(italic=True, underline=True)
eq_(Font(italic=True), bolditalic & italicunderline)
eq_(Font(bold=True, italic=True, underline=True), bolditalic | italicunderline)
eq_(Font(bold=True, underline=True), bolditalic ^ italicunderline)
fontstyle = Style(font=Font(bold=True))
fillstyle = Style(fill=Fill(background=Color(255, 0, 0, 0)))
eq_(Style(), fontstyle & fillstyle)
eq_(Style(font=Font(bold=True), fill=Fill(background=Color(255, 0, 0, 0))), fontstyle | fillstyle)
eq_(Style(font=Font(bold=True), fill=Fill(background=Color(255, 0, 0, 0))), fontstyle ^ fillstyle)
leftstyle = Style(alignment=Alignment('right', 'top'))
bottomstyle = Style(alignment=Alignment(vertical='top', rotation=15))
eq_(Style(alignment=Alignment('right', 'top', 15)), leftstyle | bottomstyle)
eq_(Style(alignment=Alignment(vertical='top')), leftstyle & bottomstyle)
eq_(Style(alignment=Alignment('right', rotation=15)), leftstyle ^ bottomstyle)
def test_str_():
font = Font(bold=True, italic=True, underline=True, strikethrough=True)
eq_(font.__repr__(), "<Font: Calibri, 11pt b i u s>")
def test_no_style_xml():
try:
import openpyxl
except ImportError:
raise nose.SkipTest('openpyxl not installed')
data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # data is a 2D array
filename = get_output_path("no_style.xlsx")
sheetname = "test"
wb = Workbook()
wb.new_sheet(sheetname, data=data)
wb.save(filename)
wbr = openpyxl.reader.excel.load_workbook(filename=filename,use_iterators=True)
mySheet = wbr.get_sheet_by_name(sheetname)
def test_unicode_with_styles():
wb = Workbook()
ws = wb.new_sheet(u"ʇǝǝɥsǝpoɔıun")
ws[1][1].value = u'Körperschaft des öffentlichen'
ws.set_col_style(2, Style(size=0))
wb.save(get_output_path("unicode-styles.xlsx"))