/usr/share/pyshared/openpyxl/tests/test_worksheet.py is in python-openpyxl 1.7.0+ds1-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 358 359 360 | # file openpyxl/tests/test_worksheet.py
# Copyright (c) 2010-2011 openpyxl
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# @license: http://www.opensource.org/licenses/mit-license.php
# @author: see AUTHORS file
# 3rd party imports
from nose.tools import eq_, raises, assert_raises
# package imports
from openpyxl.workbook import Workbook
from openpyxl.worksheet import Worksheet, Relationship, flatten
from openpyxl.writer.worksheet import write_worksheet
from openpyxl.cell import Cell, coordinate_from_string
from openpyxl.shared.exc import CellCoordinatesException, \
SheetTitleException, InsufficientCoordinatesException, \
NamedRangeException
from openpyxl.writer.worksheet import write_worksheet
class TestWorksheet(object):
@classmethod
def setup_class(cls):
cls.wb = Workbook()
def test_new_worksheet(self):
ws = Worksheet(self.wb)
eq_(self.wb, ws._parent)
def test_new_sheet_name(self):
self.wb.worksheets = []
ws = Worksheet(self.wb, title='')
eq_(repr(ws), '<Worksheet "Sheet1">')
def test_get_cell(self):
ws = Worksheet(self.wb)
cell = ws.cell('A1')
eq_(cell.get_coordinate(), 'A1')
@raises(SheetTitleException)
def test_set_bad_title(self):
Worksheet(self.wb, 'X' * 50)
def test_set_bad_title_character(self):
assert_raises(SheetTitleException, Worksheet, self.wb, '[')
assert_raises(SheetTitleException, Worksheet, self.wb, ']')
assert_raises(SheetTitleException, Worksheet, self.wb, '*')
assert_raises(SheetTitleException, Worksheet, self.wb, ':')
assert_raises(SheetTitleException, Worksheet, self.wb, '?')
assert_raises(SheetTitleException, Worksheet, self.wb, '/')
assert_raises(SheetTitleException, Worksheet, self.wb, '\\')
def test_worksheet_dimension(self):
ws = Worksheet(self.wb)
eq_('A1:A1', ws.calculate_dimension())
ws.cell('B12').value = 'AAA'
eq_('A1:B12', ws.calculate_dimension())
def test_worksheet_range(self):
ws = Worksheet(self.wb)
xlrange = ws.range('A1:C4')
assert isinstance(xlrange, tuple)
eq_(4, len(xlrange))
eq_(3, len(xlrange[0]))
def test_worksheet_named_range(self):
ws = Worksheet(self.wb)
self.wb.create_named_range('test_range', ws, 'C5')
xlrange = ws.range('test_range')
assert isinstance(xlrange, Cell)
eq_(5, xlrange.row)
@raises(NamedRangeException)
def test_bad_named_range(self):
ws = Worksheet(self.wb)
ws.range('bad_range')
@raises(NamedRangeException)
def test_named_range_wrong_sheet(self):
ws1 = Worksheet(self.wb)
ws2 = Worksheet(self.wb)
self.wb.create_named_range('wrong_sheet_range', ws1, 'C5')
ws2.range('wrong_sheet_range')
def test_cell_offset(self):
ws = Worksheet(self.wb)
eq_('C17', ws.cell('B15').offset(2, 1).get_coordinate())
def test_range_offset(self):
ws = Worksheet(self.wb)
xlrange = ws.range('A1:C4', 1, 3)
assert isinstance(xlrange, tuple)
eq_(4, len(xlrange))
eq_(3, len(xlrange[0]))
eq_('D2', xlrange[0][0].get_coordinate())
def test_cell_alternate_coordinates(self):
ws = Worksheet(self.wb)
cell = ws.cell(row=8, column=4)
eq_('E9', cell.get_coordinate())
@raises(InsufficientCoordinatesException)
def test_cell_insufficient_coordinates(self):
ws = Worksheet(self.wb)
cell = ws.cell(row=8)
def test_cell_range_name(self):
ws = Worksheet(self.wb)
self.wb.create_named_range('test_range_single', ws, 'B12')
assert_raises(CellCoordinatesException, ws.cell, 'test_range_single')
c_range_name = ws.range('test_range_single')
c_range_coord = ws.range('B12')
c_cell = ws.cell('B12')
eq_(c_range_coord, c_range_name)
eq_(c_range_coord, c_cell)
def test_garbage_collect(self):
ws = Worksheet(self.wb)
ws.cell('A1').value = ''
ws.cell('B2').value = '0'
ws.cell('C4').value = 0
ws.garbage_collect()
eq_(set(ws.get_cell_collection()), set([ws.cell('B2'), ws.cell('C4')]))
def test_hyperlink_relationships(self):
ws = Worksheet(self.wb)
eq_(len(ws.relationships), 0)
ws.cell('A1').hyperlink = "http://test.com"
eq_(len(ws.relationships), 1)
eq_("rId1", ws.cell('A1').hyperlink_rel_id)
eq_("rId1", ws.relationships[0].id)
eq_("http://test.com", ws.relationships[0].target)
eq_("External", ws.relationships[0].target_mode)
ws.cell('A2').hyperlink = "http://test2.com"
eq_(len(ws.relationships), 2)
eq_("rId2", ws.cell('A2').hyperlink_rel_id)
eq_("rId2", ws.relationships[1].id)
eq_("http://test2.com", ws.relationships[1].target)
eq_("External", ws.relationships[1].target_mode)
@raises(ValueError)
def test_bad_relationship_type(self):
rel = Relationship('bad_type')
def test_append_list(self):
ws = Worksheet(self.wb)
ws.append(['This is A1', 'This is B1'])
eq_('This is A1', ws.cell('A1').value)
eq_('This is B1', ws.cell('B1').value)
def test_append_dict_letter(self):
ws = Worksheet(self.wb)
ws.append({'A' : 'This is A1', 'C' : 'This is C1'})
eq_('This is A1', ws.cell('A1').value)
eq_('This is C1', ws.cell('C1').value)
def test_append_dict_index(self):
ws = Worksheet(self.wb)
ws.append({0 : 'This is A1', 2 : 'This is C1'})
eq_('This is A1', ws.cell('A1').value)
eq_('This is C1', ws.cell('C1').value)
@raises(TypeError)
def test_bad_append(self):
ws = Worksheet(self.wb)
ws.append("test")
def test_append_2d_list(self):
ws = Worksheet(self.wb)
ws.append(['This is A1', 'This is B1'])
ws.append(['This is A2', 'This is B2'])
vals = ws.range('A1:B2')
eq_((('This is A1', 'This is B1'),
('This is A2', 'This is B2'),), flatten(vals))
def test_rows(self):
ws = Worksheet(self.wb)
ws.cell('A1').value = 'first'
ws.cell('C9').value = 'last'
rows = ws.rows
eq_(len(rows), 9)
eq_(rows[0][0].value, 'first')
eq_(rows[-1][-1].value, 'last')
def test_cols(self):
ws = Worksheet(self.wb)
ws.cell('A1').value = 'first'
ws.cell('C9').value = 'last'
cols = ws.columns
eq_(len(cols), 3)
eq_(cols[0][0].value, 'first')
eq_(cols[-1][-1].value, 'last')
def test_auto_filter(self):
ws = Worksheet(self.wb)
ws.auto_filter = ws.range('a1:f1')
assert ws.auto_filter == 'A1:F1'
ws.auto_filter = ''
assert ws.auto_filter is None
ws.auto_filter = 'c1:g9'
assert ws.auto_filter == 'C1:G9'
def test_page_margins(self):
ws = Worksheet(self.wb)
ws.page_margins.left = 2.0
ws.page_margins.right = 2.0
ws.page_margins.top = 2.0
ws.page_margins.bottom = 2.0
ws.page_margins.header = 1.5
ws.page_margins.footer = 1.5
xml_string = write_worksheet(ws, None, None)
assert '<pageMargins left="2.00" right="2.00" top="2.00" bottom="2.00" header="1.50" footer="1.50"></pageMargins>' in xml_string
ws = Worksheet(self.wb)
xml_string = write_worksheet(ws, None, None)
assert '<pageMargins' not in xml_string
def test_merge(self):
ws = Worksheet(self.wb)
string_table = {'':'', 'Cell A1':'Cell A1', 'Cell B1':'Cell B1'}
ws.cell('A1').value = 'Cell A1'
ws.cell('B1').value = 'Cell B1'
xml_string = write_worksheet(ws, string_table, None)
assert '<v>Cell B1</v>' in xml_string
ws.merge_cells('A1:B1')
xml_string = write_worksheet(ws, string_table, None)
assert '<v>Cell B1</v>' not in xml_string
assert '<mergeCells count="1"><mergeCell ref="A1:B1"></mergeCell></mergeCells>' in xml_string
ws.unmerge_cells('A1:B1')
xml_string = write_worksheet(ws, string_table, None)
assert '<mergeCell ref="A1:B1"/>' not in xml_string
def test_freeze(self):
ws = Worksheet(self.wb)
ws.freeze_panes = ws.cell('b2')
assert ws.freeze_panes == 'B2'
ws.freeze_panes = ''
assert ws.freeze_panes is None
ws.freeze_panes = 'c5'
assert ws.freeze_panes == 'C5'
ws.freeze_panes = ws.cell('A1')
assert ws.freeze_panes is None
def test_printer_settings(self):
ws = Worksheet(self.wb)
ws.page_setup.orientation = ws.ORIENTATION_LANDSCAPE
ws.page_setup.paperSize = ws.PAPERSIZE_TABLOID
ws.page_setup.fitToPage = True
ws.page_setup.fitToHeight = 0
ws.page_setup.fitToWidth = 1
ws.page_setup.horizontalCentered = True
ws.page_setup.verticalCentered = True
xml_string = write_worksheet(ws, None, None)
assert '<pageSetup orientation="landscape" paperSize="3" fitToHeight="0" fitToWidth="1"></pageSetup>' in xml_string
assert '<pageSetUpPr fitToPage="1"></pageSetUpPr>' in xml_string
assert '<printOptions horizontalCentered="1" verticalCentered="1">' in xml_string
ws = Worksheet(self.wb)
xml_string = write_worksheet(ws, None, None)
assert "<pageSetup" not in xml_string
assert "<pageSetUpPr" not in xml_string
assert "<printOptions" not in xml_string
def test_header_footer(self):
ws = Worksheet(self.wb)
ws.header_footer.left_header.text = "Left Header Text"
ws.header_footer.center_header.text = "Center Header Text"
ws.header_footer.center_header.font_name = "Arial,Regular"
ws.header_footer.center_header.font_size = 6
ws.header_footer.center_header.font_color = "445566"
ws.header_footer.right_header.text = "Right Header Text"
ws.header_footer.right_header.font_name = "Arial,Bold"
ws.header_footer.right_header.font_size = 8
ws.header_footer.right_header.font_color = "112233"
ws.header_footer.left_footer.text = "Left Footer Text\nAnd &[Date] and &[Time]"
ws.header_footer.left_footer.font_name = "Times New Roman,Regular"
ws.header_footer.left_footer.font_size = 10
ws.header_footer.left_footer.font_color = "445566"
ws.header_footer.center_footer.text = "Center Footer Text &[Path]&[File] on &[Tab]"
ws.header_footer.center_footer.font_name = "Times New Roman,Bold"
ws.header_footer.center_footer.font_size = 12
ws.header_footer.center_footer.font_color = "778899"
ws.header_footer.right_footer.text = "Right Footer Text &[Page] of &[Pages]"
ws.header_footer.right_footer.font_name = "Times New Roman,Italic"
ws.header_footer.right_footer.font_size = 14
ws.header_footer.right_footer.font_color = "AABBCC"
xml_string = write_worksheet(ws, None, None)
assert '<headerFooter>' in xml_string
assert '<oddHeader>&L&"Calibri,Regular"&K000000Left Header Text&C&"Arial,Regular"&6&K445566Center Header Text&R&"Arial,Bold"&8&K112233Right Header Text</oddHeader>' in xml_string
assert '<oddFooter>&L&"Times New Roman,Regular"&10&K445566Left Footer Text_x000D_And &D and &T&C&"Times New Roman,Bold"&12&K778899Center Footer Text &Z&F on &A&R&"Times New Roman,Italic"&14&KAABBCCRight Footer Text &P of &N</oddFooter></headerFooter>' in xml_string
assert '</headerFooter>' in xml_string
ws = Worksheet(self.wb)
xml_string = write_worksheet(ws, None, None)
assert "<headerFooter>" not in xml_string
assert "<oddHeader>" not in xml_string
assert "<oddFooter>" not in xml_string
class TestPositioning(object):
def test_point(self):
wb = Workbook()
ws = wb.get_active_sheet()
eq_(ws.point_pos(top=40, left=150), ('C', 3))
def test_roundtrip(self):
wb = Workbook()
ws = wb.get_active_sheet()
for address in ('A1', 'D52', 'X11'):
eq_(ws.point_pos(*ws.cell(address).anchor),
coordinate_from_string(address))
|