Beispiel #1
0
 def test_cannot_copy_between_workbooks(self, WorksheetCopy):
     wb1 = Workbook()
     ws1 = wb1.active
     wb2 = Workbook()
     ws2 = wb2.active
     with pytest.raises(ValueError):
         WorksheetCopy(ws1, ws2)
Beispiel #2
0
    def test_bind(self, NamedStyle):
        style = NamedStyle(xfId=0)

        wb = Workbook()
        style.bind(wb)

        assert style._wb is wb
Beispiel #3
0
 def test_append(self, Manifest):
     from openpyxl2 import Workbook
     wb = Workbook()
     ws = wb.active
     manifest = Manifest()
     manifest.append(ws)
     assert len(manifest.Override) == 6
Beispiel #4
0
def test_write_hidden_single_worksheet():
    wb = Workbook()
    ws = wb.active
    ws.sheet_state = "hidden"
    from ..workbook import get_active_sheet
    with pytest.raises(IndexError):
        get_active_sheet(wb)
Beispiel #5
0
 def test_ctor(self, WorksheetCopy):
     wb = Workbook()
     ws1 = wb.create_sheet()
     ws2 = wb.create_sheet()
     copier = WorksheetCopy(ws1, ws2)
     assert copier.source == ws1
     assert copier.target == ws2
Beispiel #6
0
def test_read_style_iter(tmpdir):
    '''
    Test if cell styles are read properly in iter mode.
    '''
    tmpdir.chdir()
    from openpyxl2 import Workbook
    from openpyxl2.styles import Font

    FONT_NAME = "Times New Roman"
    FONT_SIZE = 15
    ft = Font(name=FONT_NAME, size=FONT_SIZE)

    wb = Workbook()
    ws = wb.worksheets[0]
    cell = ws['A1']
    cell.font = ft

    xlsx_file = "read_only_styles.xlsx"
    wb.save(xlsx_file)

    wb_iter = load_workbook(xlsx_file, read_only=True)
    ws_iter = wb_iter.worksheets[0]
    cell = ws_iter['A1']

    assert cell.font == ft
Beispiel #7
0
    def test_write(self, Manifest):
        mf = Manifest()
        from openpyxl2 import Workbook
        wb = Workbook()

        archive = ZipFile(BytesIO(), "w")
        mf._write(archive, wb)
        assert "/xl/workbook.xml" in mf.filenames
Beispiel #8
0
    def test_good_encoding(self):
        from openpyxl2 import Workbook

        wb = Workbook()
        wb.encoding = 'latin1'
        ws = wb.active
        cell = ws['A1']
        cell.value = self.test_string
Beispiel #9
0
def test_worksheet(ExcelWriter, archive):
    wb = Workbook()
    ws = wb.active
    writer = ExcelWriter(wb, archive)
    writer._write_worksheets()

    assert ws.path[1:] in archive.namelist()
    assert ws.path in writer.manifest.filenames
Beispiel #10
0
def test_write_workbook(datadir):
    datadir.chdir()
    wb = Workbook()
    content = write_workbook(wb)
    assert len(wb.rels) == 1
    with open('workbook.xml') as expected:
        diff = compare_xml(content, expected.read())
        assert diff is None, diff
Beispiel #11
0
def test_write_workbook_rels(datadir, vba, filename):
    datadir.chdir()
    wb = Workbook()
    wb.vba_archive = vba
    content = write_workbook_rels(wb)
    with open(filename) as expected:
        diff = compare_xml(content, expected.read())
        assert diff is None, diff
Beispiel #12
0
def test_read_hyperlinks_read_only(datadir, Workbook, ReadOnlyWorksheet):

    datadir.join("reader").chdir()
    filename = 'bug328_hyperlinks.xml'
    wb = Workbook()
    wb._read_only = True
    wb._data_only = True
    ws = ReadOnlyWorksheet(wb, "Sheet", "", filename, ['SOMETEXT'])
    assert ws['F2'].value is None
Beispiel #13
0
def test_chartsheet(ExcelWriter, archive):
    wb = Workbook()
    cs = wb.create_chartsheet()

    writer = ExcelWriter(wb, archive)
    writer._write_chartsheets()

    assert cs.path in writer.manifest.filenames
    assert cs.path[1:] in writer._archive.namelist()
Beispiel #14
0
def test_write_hyperlink_image_rels(Workbook, Image, datadir):
    datadir.chdir()
    wb = Workbook()
    ws = wb.create_sheet()
    ws['A1'].value = "test"
    ws['A1'].hyperlink = "http://test.com/"
    i = Image("plain.png")
    ws.add_image(i)
    raise ValueError("Resulting file is invalid")
Beispiel #15
0
def test_duplicate_chart(ExcelWriter, archive, Workbook):
    from openpyxl2.chart import PieChart
    pc = PieChart()
    wb = Workbook()
    writer = ExcelWriter(wb, archive)

    writer._charts = [pc]*2
    with pytest.raises(InvalidFileException):
        writer._write_charts()
Beispiel #16
0
    def test_media(self, Manifest, file, registration):
        from openpyxl2 import Workbook
        wb = Workbook()

        manifest = Manifest()
        manifest._register_mimetypes([file])
        xml = tostring(manifest.Default[-1].to_tree())
        diff = compare_xml(xml, registration)
        assert diff is None, diff
Beispiel #17
0
def DummyWorkbook():
    class Workbook:
        epoch = None
        _cell_styles = [StyleArray([0, 0, 0, 0, 0, 0, 0, 0, 0])]

        def __init__(self):
            self.sheetnames = []

    return Workbook()
Beispiel #18
0
    def test_recalculate(self, NamedStyle, attr, key, collection, expected):
        style = NamedStyle(xfId=0)
        wb = Workbook()
        wb._number_formats.append("###")
        style.bind(wb)
        style._style = StyleArray([1, 1, 1, 1, 1, 1, 1, 1, 1])

        obj = getattr(wb, collection)[0]
        setattr(style, attr, obj)
        assert getattr(style._style, key) == expected
Beispiel #19
0
    def test_bad_encoding(self):
        from openpyxl2 import Workbook

        wb = Workbook()
        ws = wb.active
        cell = ws['A1']
        with pytest.raises(UnicodeDecodeError):
            cell.check_string(self.test_string)
        with pytest.raises(UnicodeDecodeError):
            cell.value = self.test_string
Beispiel #20
0
def test_drawing(ExcelWriter, archive):
    wb = Workbook()
    ws = wb.active

    drawing = SpreadsheetDrawing()

    writer = ExcelWriter(wb, archive)
    writer._write_drawing(drawing)
    assert drawing.path == '/xl/drawings/drawing1.xml'
    assert drawing.path[1:] in archive.namelist()
    assert drawing.path in writer.manifest.filenames
Beispiel #21
0
def test_write_auto_filter(datadir):
    datadir.chdir()
    wb = Workbook()
    ws = wb.active
    ws['F42'].value = 'hello'
    ws.auto_filter.ref = 'A1:F1'

    content = write_workbook(wb)
    with open('workbook_auto_filter.xml') as expected:
        diff = compare_xml(content, expected.read())
        assert diff is None, diff
Beispiel #22
0
def test_comment(ExcelWriter, archive):
    from openpyxl2.comments import Comment
    wb = Workbook()
    ws = wb.active
    ws['B5'].comment = Comment("A comment", "The Author")

    writer = ExcelWriter(None, archive)
    writer._write_comment(ws)

    assert archive.namelist() == ['xl/comments/comment1.xml', 'xl/drawings/commentsDrawing1.vml']
    assert '/xl/comments/comment1.xml' in writer.manifest.filenames
    assert ws.legacy_drawing == 'xl/drawings/commentsDrawing1.vml'
Beispiel #23
0
def test_write_workbook_protection(datadir):
    from ...workbook.protection import WorkbookProtection

    datadir.chdir()
    wb = Workbook()
    wb.security = WorkbookProtection(lockStructure=True)
    wb.security.set_workbook_password('ABCD', already_hashed=True)

    content = write_workbook(wb)
    with open('workbook_protection.xml') as expected:
        diff = compare_xml(content, expected.read())
        assert diff is None, diff
Beispiel #24
0
def test_tables(ExcelWriter, archive):
    wb = Workbook()
    ws = wb.active
    ws.append(list(ascii_letters))
    ws._rels = []
    t = Table(displayName="Table1", ref="A1:D10")
    ws.add_table(t)


    writer = ExcelWriter(wb, archive)
    writer._write_worksheets()

    assert t.path[1:] in archive.namelist()
    assert t.path in writer.manifest.filenames
Beispiel #25
0
def test_write_named_range():
    wb = Workbook()
    ws = wb.active
    ws.title = "Test Sheet"
    wb.create_named_range("test_range", ws, value="A1:B5")

    xml = tostring(wb.defined_names.to_tree())
    expected = """
    <definedNames>
     <definedName name="test_range">'Test Sheet'!A1:B5</definedName>
    </definedNames>
    """
    diff = compare_xml(xml, expected)
    assert diff is None, diff
Beispiel #26
0
def test_write_root_rels():
    from ..workbook import write_root_rels

    wb = Workbook()
    xml = write_root_rels(wb)
    expected = """
    <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
      <Relationship Id="rId1" Target="xl/workbook.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"/>
      <Relationship Id="rId2" Target="docProps/core.xml" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties"/>
      <Relationship Id="rId3" Target="docProps/app.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties"/>
    </Relationships>
    """
    diff = compare_xml(xml, expected)
    assert diff is None, diff
Beispiel #27
0
    def test_col_style(self, ColumnDimension):
        from ..worksheet import Worksheet
        from openpyxl2 import Workbook
        from openpyxl2.styles import Font

        ws = Worksheet(Workbook())
        cd = ColumnDimension(ws, index="A")
        cd.font = Font(color="FF0000")
        cd.reindex()
        col = cd.to_tree()
        xml = tostring(col)
        expected = """<col max="1" min="1" style="1" />"""
        diff = compare_xml(xml, expected)
        assert diff is None, diff
Beispiel #28
0
def test_write_chart(ExcelWriter, archive):
    wb = Workbook()
    ws = wb.active

    chart = BarChart()
    ws.add_chart(chart)

    writer = ExcelWriter(wb, archive)
    writer._write_worksheets()
    assert 'xl/worksheets/sheet1.xml' in archive.namelist()
    assert ws.path in writer.manifest.filenames

    rel = ws._rels["rId1"]
    assert dict(rel) == {'Id': 'rId1', 'Target': '/xl/drawings/drawing1.xml',
                         'Type':
                         'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing'}
Beispiel #29
0
def _comment_list():
    from ..comments import Comment
    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(comment)

    return comments
Beispiel #30
0
def test_save():
    from tempfile import NamedTemporaryFile
    filename = NamedTemporaryFile(delete=False)
    from openpyxl2.workbook import Workbook
    from ..excel import save_dump
    wb = Workbook(write_only=True)
    save_dump(wb, filename)

    archive = ZipFile(filename)
    assert archive.namelist() == [
        '_rels/.rels',
        'docProps/app.xml',
        'docProps/core.xml',
        'xl/theme/theme1.xml',
        'xl/worksheets/sheet1.xml',
        'xl/sharedStrings.xml',
        'xl/styles.xml',
        'xl/workbook.xml',
        'xl/_rels/workbook.xml.rels',
        '[Content_Types].xml'
    ]