/usr/lib/python2.7/dist-packages/openpyxl/formatting/tests/test_formatting.py is in python-openpyxl 2.3.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | from __future__ import absolute_import
# Copyright (c) 2010-2015 openpyxl
# compatibility imports
from openpyxl.compat import OrderedDict
# package imports
from openpyxl.reader.excel import load_workbook
from openpyxl.xml.functions import tostring
from openpyxl.writer.worksheet import write_conditional_formatting
from openpyxl.styles import Border, Side, PatternFill, Color, Font, fills, borders, colors
from openpyxl.styles.differential import DifferentialStyle
from openpyxl.formatting import ConditionalFormatting
from openpyxl.formatting.rule import ColorScaleRule, CellIsRule, FormulaRule, Rule
# test imports
import pytest
from openpyxl.tests.helper import compare_xml
@pytest.fixture
def rules():
class DummyRule:
def __init__(self, priority):
self.priority = priority
return OrderedDict(
[
('H1:H10', [DummyRule(23)]),
('Q1:Q10', [DummyRule(14)]),
('G1:G10', [DummyRule(24)]),
('F1:F10', [DummyRule(25)]),
('O1:O10', [DummyRule(16)]),
('K1:K10', []),
('T1:T10', [DummyRule(11)]),
('X1:X10', [DummyRule(7)]),
('R1:R10', [DummyRule(13)]),
('C1:C10', [DummyRule(28)]),
('J1:J10', [DummyRule(21)]),
('E1:E10', [DummyRule(26)]),
('I1:I10', [DummyRule(22)]),
('Z1:Z10', [DummyRule(5)]),
('V1:V10', [DummyRule(9)]),
('AC1:AC10', [DummyRule(2)]),
('L1:L10', []),
('N1:N10', [DummyRule(17)]),
('AA1:AA10', [DummyRule(4)]),
('M1:M10', []),
('Y1:Y10', [DummyRule(6)]),
('B1:B10', [DummyRule(29)]),
('P1:P10', [DummyRule(15)]),
('W1:W10', [DummyRule(8)]),
('AB1:AB10', [DummyRule(3)]),
('A1:A1048576', [DummyRule(29)]),
('S1:S10', [DummyRule(12)]),
('D1:D10', [DummyRule(27)])
]
)
def test_unpack_rules(rules):
from ..formatting import unpack_rules
assert list(unpack_rules(rules)) == [
('H1:H10', 0, 23),
('Q1:Q10', 0, 14),
('G1:G10', 0, 24),
('F1:F10', 0, 25),
('O1:O10', 0, 16),
('T1:T10', 0, 11),
('X1:X10', 0, 7),
('R1:R10', 0, 13),
('C1:C10', 0, 28),
('J1:J10', 0, 21),
('E1:E10', 0, 26),
('I1:I10', 0, 22),
('Z1:Z10', 0, 5),
('V1:V10', 0, 9),
('AC1:AC10', 0, 2),
('N1:N10', 0, 17),
('AA1:AA10', 0, 4),
('Y1:Y10', 0, 6),
('B1:B10', 0, 29),
('P1:P10', 0, 15),
('W1:W10', 0, 8),
('AB1:AB10', 0, 3),
('A1:A1048576',0 ,29),
('S1:S10', 0, 12),
('D1:D10', 0, 27),
]
def test_fix_priorities(rules):
from ..formatting import unpack_rules, ConditionalFormatting
cf = ConditionalFormatting()
cf.cf_rules = rules
cf._fix_priorities()
assert cf.max_priority == 25
assert list(unpack_rules(cf.cf_rules)) == [
('H1:H10', 0, 18),
('Q1:Q10', 0, 12),
('G1:G10', 0, 19),
('F1:F10', 0, 20),
('O1:O10', 0, 14),
('T1:T10', 0, 9),
('X1:X10', 0, 6),
('R1:R10', 0, 11),
('C1:C10', 0, 23),
('J1:J10', 0, 16),
('E1:E10', 0, 21),
('I1:I10', 0, 17),
('Z1:Z10', 0, 4),
('V1:V10', 0, 8),
('AC1:AC10', 0, 1),
('N1:N10', 0, 15),
('AA1:AA10', 0, 3),
('Y1:Y10', 0, 5),
('B1:B10', 0, 24),
('P1:P10', 0, 13),
('W1:W10', 0, 7),
('AB1:AB10', 0, 2),
('A1:A1048576', 0, 25),
('S1:S10', 0, 10),
('D1:D10', 0, 22),
]
class DummyWorkbook():
def __init__(self):
self._differential_styles = []
self.worksheets = []
class DummyWorksheet():
def __init__(self):
self.conditional_formatting = ConditionalFormatting()
self.parent = DummyWorkbook()
class TestConditionalFormatting(object):
def setup(self):
self.ws = DummyWorksheet()
def test_conditional_formatting_customRule(self):
worksheet = self.ws
worksheet.conditional_formatting.add('C1:C10',
Rule(**{'type': 'expression', 'formula': ['ISBLANK(C1)'],
'stopIfTrue': '1',}
)
)
cfs = write_conditional_formatting(worksheet)
xml = b""
for cf in cfs:
xml += tostring(cf)
diff = compare_xml(xml, """
<conditionalFormatting sqref="C1:C10">
<cfRule type="expression" stopIfTrue="1" priority="1">
<formula>ISBLANK(C1)</formula>
</cfRule>
</conditionalFormatting>
""")
assert diff is None, diff
def test_write_conditional_formatting(self):
ws = self.ws
cf = ConditionalFormatting()
ws.conditional_formatting = cf
fill = PatternFill(start_color=Color('FFEE1111'),
end_color=Color('FFEE1111'),
patternType=fills.FILL_SOLID)
font = Font(name='Arial', size=12, bold=True,
underline=Font.UNDERLINE_SINGLE)
border = Border(top=Side(border_style=borders.BORDER_THIN,
color=Color(colors.DARKYELLOW)),
bottom=Side(border_style=borders.BORDER_THIN,
color=Color(colors.BLACK)))
cf.add('C1:C10', FormulaRule(formula=['ISBLANK(C1)'], font=font, border=border, fill=fill))
cf.add('D1:D10', FormulaRule(formula=['ISBLANK(D1)'], fill=fill))
from openpyxl.writer.worksheet import write_conditional_formatting
for _ in write_conditional_formatting(ws):
pass # exhaust generator
wb = ws.parent
assert len(wb._differential_styles) == 2
ft1, ft2 = wb._differential_styles
assert ft1.font == font
assert ft1.border == border
assert ft1.fill == fill
assert ft2.fill == fill
def test_conditional_font(self):
"""Test to verify font style written correctly."""
ws = self.ws
cf = ConditionalFormatting()
ws.conditional_formatting = cf
# Create cf rule
redFill = PatternFill(start_color=Color('FFEE1111'),
end_color=Color('FFEE1111'),
patternType=fills.FILL_SOLID)
whiteFont = Font(color=Color("FFFFFFFF"))
ws.conditional_formatting.add('A1:A3',
CellIsRule(operator='equal', formula=['"Fail"'], stopIfTrue=False,
font=whiteFont, fill=redFill))
from openpyxl.writer.worksheet import write_conditional_formatting
# First, verify conditional formatting xml
cfs = write_conditional_formatting(ws)
xml = b""
for cf in cfs:
xml += tostring(cf)
diff = compare_xml(xml, """
<conditionalFormatting sqref="A1:A3">
<cfRule dxfId="0" operator="equal" priority="1" type="cellIs" stopIfTrue="0">
<formula>"Fail"</formula>
</cfRule>
</conditionalFormatting>
""")
assert diff is None, diff
def test_conditional_formatting_read(datadir):
datadir.chdir()
reference_file = 'conditional-formatting.xlsx'
wb = load_workbook(reference_file)
ws = wb.active
rules = ws.conditional_formatting.cf_rules
assert len(rules) == 30
# First test the conditional formatting rules read
rule = rules['A1:A1048576'][0]
assert dict(rule) == {'priority':'30', 'type': 'colorScale', }
rule = rules['B1:B10'][0]
assert dict(rule) == {'priority': '29', 'type': 'colorScale'}
rule = rules['C1:C10'][0]
assert dict(rule) == {'priority': '28', 'type': 'colorScale'}
rule = rules['D1:D10'][0]
assert dict(rule) == {'priority': '27', 'type': 'colorScale', }
rule = rules['E1:E10'][0]
assert dict(rule) == {'priority': '26', 'type': 'colorScale', }
rule = rules['F1:F10'][0]
assert dict(rule) == {'priority': '25', 'type': 'colorScale', }
rule = rules['G1:G10'][0]
assert dict(rule) == {'priority': '24', 'type': 'colorScale', }
rule = rules['H1:H10'][0]
assert dict(rule) == {'priority': '23', 'type': 'colorScale', }
rule = rules['I1:I10'][0]
assert dict(rule) == {'priority': '22', 'type': 'colorScale', }
rule = rules['J1:J10'][0]
assert dict(rule) == {'priority': '21', 'type': 'colorScale', }
rule = rules['K1:K10'][0]
assert dict(rule) == {'priority': '20', 'type': 'dataBar'}
rule = rules['L1:L10'][0]
assert dict(rule) == {'priority': '19', 'type': 'dataBar'}
rule = rules['M1:M10'][0]
assert dict(rule) == {'priority': '18', 'type': 'dataBar'}
rule = rules['N1:N10'][0]
assert dict(rule) == {'priority': '17', 'type': 'iconSet'}
rule = rules['O1:O10'][0]
assert dict(rule) == {'priority': '16', 'type': 'iconSet'}
rule = rules['P1:P10'][0]
assert dict(rule) == {'priority': '15', 'type': 'iconSet'}
rule = rules['Q1:Q10'][0]
assert dict(rule) == {'text': '3', 'priority': '14', 'dxfId': '27',
'operator': 'containsText', 'type': 'containsText'}
assert rule.dxf == DifferentialStyle(font=Font(color='FF9C0006'),
fill=PatternFill(bgColor='FFFFC7CE')
)
rule = rules['R1:R10'][0]
assert dict(rule) == {'operator': 'between', 'dxfId': '26', 'type':
'cellIs', 'priority': '13'}
assert rule.dxf == DifferentialStyle(font=Font(color='FF9C6500'),
fill=PatternFill(bgColor='FFFFEB9C'))
rule = rules['S1:S10'][0]
assert dict(rule) == {'priority': '12', 'dxfId': '25', 'percent': '1',
'type': 'top10', 'rank': '10'}
rule = rules['T1:T10'][0]
assert dict(rule) == {'priority': '11', 'dxfId': '24', 'type': 'top10',
'rank': '4', 'bottom': '1'}
rule = rules['U1:U10'][0]
assert dict(rule) == {'priority': '10', 'dxfId': '23', 'type':
'aboveAverage'}
rule = rules['V1:V10'][0]
assert dict(rule) == {'aboveAverage': '0', 'dxfId': '22', 'type':
'aboveAverage', 'priority': '9'}
rule = rules['W1:W10'][0]
assert dict(rule) == {'priority': '8', 'dxfId': '21', 'type':
'aboveAverage', 'equalAverage': '1'}
rule = rules['X1:X10'][0]
assert dict(rule) == {'aboveAverage': '0', 'dxfId': '20', 'priority': '7',
'type': 'aboveAverage', 'equalAverage': '1'}
rule = rules['Y1:Y10'][0]
assert dict(rule) == {'priority': '6', 'dxfId': '19', 'type':
'aboveAverage', 'stdDev': '1'}
rule = rules['Z1:Z10'][0]
assert dict(rule)== {'aboveAverage': '0', 'dxfId': '18', 'type':
'aboveAverage', 'stdDev': '1', 'priority': '5'}
assert rule.dxf == DifferentialStyle(font=Font(b=True, i=True, color='FF9C0006'),
fill=PatternFill(bgColor='FFFFC7CE'),
border=Border(
left=Side(style='thin', color=Color(theme=5)),
right=Side(style='thin', color=Color(theme=5)),
top=Side(style='thin', color=Color(theme=5)),
bottom=Side(style='thin', color=Color(theme=5))
)
)
rule = rules['AA1:AA10'][0]
assert dict(rule) == {'priority': '4', 'dxfId': '17', 'type':
'aboveAverage', 'stdDev': '2'}
rule = rules['AB1:AB10'][0]
assert dict(rule) == {'priority': '3', 'dxfId': '16', 'type':
'duplicateValues'}
rule = rules['AC1:AC10'][0]
assert dict(rule) == {'priority': '2', 'dxfId': '15', 'type':
'uniqueValues'}
rule = rules['AD1:AD10'][0]
assert dict(rule) == {'priority': '1', 'dxfId': '14', 'type': 'expression',}
|