Beispiel #1
0
def test_simple_styles(datadir):
    import datetime
    from openpyexcel import Workbook
    from ..protection import Protection
    from .. import numbers
    from ..stylesheet import write_stylesheet
    wb = Workbook()
    wb.guess_types = True
    ws = wb.active
    now = datetime.date.today()
    for idx, v in enumerate(
        ['12.34%', now, 'This is a test', '31.31415', None], 1):
        ws.append([v])
        _ = ws.cell(column=1, row=idx).style_id

    # set explicit formats
    ws['D9'].number_format = numbers.FORMAT_NUMBER_00
    ws['D9'].protection = Protection(locked=True)
    ws['D9'].style_id
    ws['E1'].protection = Protection(hidden=True)
    ws['E1'].style_id

    assert len(wb._cell_styles) == 5
    stylesheet = write_stylesheet(wb)

    datadir.chdir()
    with open('simple-styles.xml') as reference_file:
        expected = reference_file.read()
    xml = tostring(stylesheet)
    diff = compare_xml(xml, expected)
    assert diff is None, diff
Beispiel #2
0
def test_add_invalid_worksheet_class_instance():
    class AlternativeWorksheet(object):
        def __init__(self, parent_workbook, title=None):
            self.parent_workbook = parent_workbook
            if not title:
                title = 'AlternativeSheet'
            self.title = title

    wb = Workbook()
    ws = AlternativeWorksheet(parent_workbook=wb)
    with pytest.raises(TypeError):
        wb._add_sheet(worksheet=ws)
Beispiel #3
0
def test_set_active_by_index():
    wb = Workbook()
    names = [
        'Sheet',
        'Sheet1',
        'Sheet2',
    ]
    for n in names:
        wb.create_sheet(n)

    for idx, name in enumerate(names):
        wb.active = idx
        assert wb.active == wb.worksheets[idx]
Beispiel #4
0
def test_set_active_by_sheet():
    wb = Workbook()
    names = [
        'Sheet',
        'Sheet1',
        'Sheet2',
    ]
    for n in names:
        wb.create_sheet(n)

    for n in names:
        sheet = wb[n]
        wb.active = sheet
        assert wb.active == wb[n]
Beispiel #5
0
def test_no_styles():
    from ..stylesheet import apply_stylesheet
    from zipfile import ZipFile
    from io import BytesIO
    from openpyexcel.workbook import Workbook
    wb1 = wb2 = Workbook()
    archive = ZipFile(BytesIO(), "a")
    apply_stylesheet(archive, wb1)
    assert wb1._cell_styles == wb2._cell_styles
    assert wb2._named_styles == wb2._named_styles
Beispiel #6
0
def test_remove_named_range():
    wb = Workbook()
    new_sheet = wb.create_sheet()
    wb.create_named_range('test_nr', new_sheet, 'A1')
    del wb.defined_names['test_nr']
    named_ranges_list = wb.get_named_ranges()
    assert 'test_nr' not in named_ranges_list
Beispiel #7
0
def test_add_named_range():
    wb = Workbook()
    new_sheet = wb.create_sheet()
    named_range = DefinedName('test_nr')
    named_range.value = "Sheet!A1"
    wb.add_named_range(named_range)
    named_ranges_list = wb.get_named_ranges()
    assert named_range in named_ranges_list
Beispiel #8
0
def test_write_worksheet(Stylesheet):
    from openpyexcel import Workbook
    wb = Workbook()
    from ..stylesheet import write_stylesheet
    node = write_stylesheet(wb)
    xml = tostring(node)
    expected = """
    <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
      <numFmts count="0" />
      <fonts count="1">
        <font>
          <name val="Calibri"></name>
          <family val="2"></family>
          <color theme="1"></color>
          <sz val="11"></sz>
          <scheme val="minor"></scheme>
        </font>
      </fonts>
      <fills count="2">
        <fill>
          <patternFill></patternFill>
        </fill>
        <fill>
          <patternFill patternType="gray125"></patternFill>
        </fill>
      </fills>
      <borders count="1">
        <border>
          <left></left>
          <right></right>
          <top></top>
          <bottom></bottom>
          <diagonal></diagonal>
        </border>
      </borders>
      <cellStyleXfs count="1">
        <xf borderId="0" fillId="0" fontId="0" numFmtId="0"></xf>
      </cellStyleXfs>
      <cellXfs count="1">
        <xf borderId="0" fillId="0" fontId="0" numFmtId="0" pivotButton="0" quotePrefix="0" xfId="0"></xf>
      </cellXfs>
      <cellStyles count="1">
        <cellStyle builtinId="0" hidden="0" name="Normal" xfId="0"></cellStyle>
      </cellStyles>
    <tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleLight16"/>
    </styleSheet>
    """
    diff = compare_xml(xml, expected)
    assert diff is None, diff
Beispiel #9
0
    def test_split_named_styles(self, Stylesheet):
        from openpyexcel.workbook import Workbook
        import copy
        wb = Workbook()
        new_style = copy.copy(wb._named_styles[0])
        # Set to an incorrect xfId, which will happen when we load a
        # worksheet with unused styles. The unused styles never make it to the
        # workbook, so the xfIds will be off by the trimmed number.
        new_style.name = "Regression647"
        wb._named_styles.append(new_style)

        stylesheet = Stylesheet()
        stylesheet._split_named_styles(wb)

        assert stylesheet.cellStyles.cellStyle[-1].name == "Regression647"
        assert stylesheet.cellStyles.cellStyle[
            -1].xfId < stylesheet.cellStyleXfs.count
Beispiel #10
0
def create_comments():
    wb = Workbook()
    ws = wb.active
    comment1 = Comment("text", "author")
    comment2 = Comment("text2", "author2")
    comment3 = Comment("text3", "author3")
    ws["B2"].comment = comment1
    ws["C7"].comment = comment2
    ws["D9"].comment = comment3

    comments = []
    for coord, cell in sorted(ws._cells.items()):
        if cell._comment is not None:
            comment = CommentRecord.from_cell(cell)
            comments.append((cell.coordinate, comment))

    return comments
Beispiel #11
0
def test_contains(Workbook):
    wb = Workbook()
    assert "Sheet" in wb
    assert "NotThere" not in wb
Beispiel #12
0
def test_iter(Workbook):
    wb = Workbook()
    for ws in wb:
        pass
    assert ws.title == "Sheet"
Beispiel #13
0
def test_get_active_sheet():
    wb = Workbook()
    assert wb.active == wb.worksheets[0]
Beispiel #14
0
 def test_immutable_builtins(self):
     wb1 = Workbook()
     wb2 = Workbook()
     normal = wb1._named_styles['Normal']
     normal.font.color = "FF0000"
     assert wb2._named_styles['Normal'].font.color.index == 1
Beispiel #15
0
 def test_cannot_copy_writeonly(self):
     wb = Workbook(write_only=True)
     ws = wb.create_sheet()
     with pytest.raises(ValueError):
         wb.copy_worksheet(ws)
Beispiel #16
0
def test_get_sheet_names():
    wb = Workbook()
    names = ['Sheet', 'Sheet1', 'Sheet2', 'Sheet3', 'Sheet4', 'Sheet5']
    for count in range(5):
        wb.create_sheet(0)
    assert wb.sheetnames == names
Beispiel #17
0
def test_set_invalid_child_as_active():
    wb1 = Workbook()
    wb2 = Workbook()
    ws2 = wb2['Sheet']
    with pytest.raises(ValueError):
        wb1.active = ws2
Beispiel #18
0
 def test_named_styles(self):
     wb = Workbook()
     assert wb.named_styles == ['Normal']
Beispiel #19
0
 def test_template(self, has_vba, as_template, content_type):
     from openpyexcel.workbook import Workbook
     wb = Workbook()
     wb.vba_archive = has_vba
     wb.template = as_template
     assert wb.mime_type == content_type
Beispiel #20
0
def test_remove_sheet_with_names():
    wb = Workbook()
    new_sheet = wb.create_sheet()
    wb.create_named_range('test_nr', new_sheet, 'A1', 1)
    del wb['Sheet1']
    assert wb.defined_names.definedName == []
Beispiel #21
0
def test_index():
    wb = Workbook()
    new_sheet = wb.create_sheet()
    sheet_index = wb.index(new_sheet)
    assert sheet_index == 1
Beispiel #22
0
def test_get_named_range():
    wb = Workbook()
    new_sheet = wb.create_sheet()
    wb.create_named_range('test_nr', new_sheet, 'A1')
    assert wb.defined_names['test_nr'].value == "'Sheet1'!A1"
Beispiel #23
0
def test_set_invalid_active_index():
    wb = Workbook()
    with pytest.raises(ValueError):
        wb.active = 1
Beispiel #24
0
 def test_worksheet_copy_name(self, title, copy):
     wb = Workbook()
     ws1 = wb.active
     ws1.title = title
     ws2 = wb.copy_worksheet(ws1)
     assert ws2.title == copy
Beispiel #25
0
def test_set_invalid_sheet_by_name():
    wb = Workbook()
    with pytest.raises(TypeError):
        wb.active = "Sheet"
Beispiel #26
0
 def test_cannot_copy_readonly(self):
     wb = Workbook()
     ws = wb.active
     wb._read_only = True
     with pytest.raises(ValueError):
         wb.copy_worksheet(ws)
Beispiel #27
0
def test_set_hidden_sheet_as_active():
    wb = Workbook()
    ws = wb.create_sheet()
    ws.sheet_state = 'hidden'
    with pytest.raises(ValueError):
        wb.active = ws
Beispiel #28
0
def test_get_named_ranges():
    wb = Workbook()
    assert wb.get_named_ranges() == wb.defined_names.definedName
Beispiel #29
0
def test_close_write(wo):
    from openpyexcel.workbook import Workbook
    wb = Workbook(write_only=wo)
    wb.close()
Beispiel #30
0
 def test_worksheet_copy(self):
     wb = Workbook()
     ws1 = wb.active
     ws2 = wb.copy_worksheet(ws1)
     assert ws2 is not None