/usr/lib/python2.7/dist-packages/openpyxl/reader/tests/test_workbook.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 | from __future__ import absolute_import
# Copyright (c) 2010-2015 openpyxl
from io import BytesIO
from zipfile import ZipFile
import pytest
from openpyxl.xml.constants import (
ARC_WORKBOOK,
ARC_CONTENT_TYPES,
ARC_WORKBOOK_RELS,
REL_NS,
)
from openpyxl.utils.datetime import (
CALENDAR_WINDOWS_1900,
CALENDAR_MAC_1904,
)
@pytest.fixture()
def DummyArchive():
body = BytesIO()
archive = ZipFile(body, "w")
return archive
def test_hidden_sheets(datadir, DummyArchive):
from .. workbook import read_sheets
datadir.chdir()
archive = DummyArchive
with open("hidden_sheets.xml") as src:
archive.writestr(ARC_WORKBOOK, src.read())
sheets = read_sheets(archive)
assert list(sheets) == [
{'id': 'rId1', 'name': 'Blatt1', 'sheetId': '1'},
{'id': 'rId2', 'sheetId': '2', 'name': 'Blatt2', 'state': 'hidden'},
{'id': 'rId3', 'state': 'hidden', 'sheetId': '3', 'name': 'Blatt3'},
]
@pytest.mark.parametrize("excel_file, expected", [
("bug137.xlsx", [
{'state':'visible', 'path': 'xl/worksheets/sheet1.xml', 'title': 'Sheet1', 'type':'%s/worksheet' % REL_NS, 'sheet_id':'1'}
]
),
("contains_chartsheets.xlsx", [
{'state':'visible', 'path': 'xl/worksheets/sheet1.xml', 'title': 'data', 'type':'%s/worksheet' % REL_NS, 'sheet_id':'1'},
{'state':'visible', 'path': 'xl/worksheets/sheet2.xml', 'title': 'moredata', 'type':'%s/worksheet' % REL_NS, 'sheet_id':'2'},
]),
("bug304.xlsx", [
{'state':'visible', 'path': 'xl/worksheets/sheet3.xml', 'title': 'Sheet1', 'type':'%s/worksheet' % REL_NS, 'sheet_id':'1'},
{'state':'visible', 'path': 'xl/worksheets/sheet2.xml', 'title': 'Sheet2', 'type':'%s/worksheet' % REL_NS, 'sheet_id':'2'},
{'state':'visible', 'path': 'xl/worksheets/sheet.xml', 'title': 'Sheet3', 'type':'%s/worksheet' % REL_NS, 'sheet_id':'3'},
])
]
)
def test_detect_worksheets(datadir, excel_file, expected):
from openpyxl.reader.excel import detect_worksheets
datadir.chdir()
archive = ZipFile(excel_file)
assert list(detect_worksheets(archive)) == expected
@pytest.mark.parametrize("excel_file, expected", [
("bug137.xlsx", {
"rId1": {'path': 'xl/chartsheets/sheet1.xml', 'type':'%s/chartsheet' % REL_NS, },
"rId2": {'path': 'xl/worksheets/sheet1.xml', 'type':'%s/worksheet' % REL_NS, },
"rId3": {'path': 'xl/theme/theme1.xml', 'type':'%s/theme' % REL_NS},
"rId4": {'path': 'xl/styles.xml', 'type':'%s/styles' % REL_NS},
"rId5": {'path': 'xl/sharedStrings.xml', 'type':'%s/sharedStrings' % REL_NS}
}),
("bug304.xlsx", {
'rId1': {'path': 'xl/worksheets/sheet3.xml', 'type':'%s/worksheet' % REL_NS},
'rId2': {'path': 'xl/worksheets/sheet2.xml', 'type':'%s/worksheet' % REL_NS},
'rId3': {'path': 'xl/worksheets/sheet.xml', 'type':'%s/worksheet' % REL_NS},
'rId4': {'path': 'xl/theme/theme.xml', 'type':'%s/theme' % REL_NS},
'rId5': {'path': 'xl/styles.xml', 'type':'%s/styles' % REL_NS},
'rId6': {'path': '../customXml/item1.xml', 'type':'%s/customXml' % REL_NS},
'rId7': {'path': '../customXml/item2.xml', 'type':'%s/customXml' % REL_NS},
'rId8': {'path': '../customXml/item3.xml', 'type':'%s/customXml' % REL_NS}
}),
]
)
def test_read_rels(datadir, excel_file, expected):
from openpyxl.reader.workbook import read_rels
datadir.chdir()
archive = ZipFile(excel_file)
assert dict(read_rels(archive)) == expected
@pytest.mark.parametrize("workbook_file, expected", [
("bug137_workbook.xml",
[
{'sheetId': '4', 'id': 'rId1', 'name': 'Chart1'},
{'name': 'Sheet1', 'sheetId': '1', 'id': 'rId2'},
]
),
("bug304_workbook.xml",
[
{'id': 'rId1', 'name': 'Sheet1', 'sheetId': '1'},
{'name': 'Sheet2', 'id': 'rId2', 'sheetId': '2'},
{'id': 'rId3', 'sheetId': '3', 'name': 'Sheet3'},
]
)
])
def test_read_sheets(datadir, DummyArchive, workbook_file, expected):
from openpyxl.reader.workbook import read_sheets
datadir.chdir()
archive = DummyArchive
with open(workbook_file) as src:
archive.writestr(ARC_WORKBOOK, src.read())
assert list(read_sheets(archive)) == expected
def test_read_content_types(datadir, DummyArchive):
from openpyxl.reader.workbook import read_content_types
archive = DummyArchive
datadir.chdir()
with open("content_types.xml") as src:
archive.writestr(ARC_CONTENT_TYPES, src.read())
assert list(read_content_types(archive)) == [
('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml', '/xl/workbook.xml'),
('application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml', '/xl/worksheets/sheet1.xml'),
('application/vnd.openxmlformats-officedocument.spreadsheetml.chartsheet+xml', '/xl/chartsheets/sheet1.xml'),
('application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml', '/xl/worksheets/sheet2.xml',),
('application/vnd.openxmlformats-officedocument.theme+xml', '/xl/theme/theme1.xml'),
('application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml', '/xl/styles.xml'),
('application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml', '/xl/sharedStrings.xml'),
('application/vnd.openxmlformats-officedocument.drawing+xml', '/xl/drawings/drawing1.xml'),
('application/vnd.openxmlformats-officedocument.drawingml.chart+xml','/xl/charts/chart1.xml'),
('application/vnd.openxmlformats-officedocument.drawing+xml', '/xl/drawings/drawing2.xml'),
('application/vnd.openxmlformats-officedocument.drawingml.chart+xml', '/xl/charts/chart2.xml'),
('application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml', '/xl/calcChain.xml'),
('application/vnd.openxmlformats-package.core-properties+xml', '/docProps/core.xml'),
('application/vnd.openxmlformats-officedocument.extended-properties+xml', '/docProps/app.xml')
]
def test_missing_content_type(datadir, DummyArchive):
from .. workbook import detect_worksheets
archive = DummyArchive
datadir.chdir()
with open("bug181_content_types.xml") as src:
archive.writestr(ARC_CONTENT_TYPES, src.read())
with open("bug181_workbook.xml") as src:
archive.writestr(ARC_WORKBOOK, src.read())
with open("bug181_workbook.xml.rels") as src:
archive.writestr(ARC_WORKBOOK_RELS, src.read())
sheets = list(detect_worksheets(archive))
assert sheets == [{'state':'visible', 'path': 'xl/worksheets/sheet1.xml', 'title': 'Sheet 1', 'sheet_id':'1',
'type':'%s/worksheet' % REL_NS}]
def test_read_workbook_with_no_core_properties(datadir, Workbook):
from openpyxl.workbook import DocumentProperties
from openpyxl.reader.excel import load_workbook
datadir.chdir()
wb = load_workbook('empty_with_no_properties.xlsx')
assert isinstance(wb.properties, DocumentProperties)
@pytest.mark.parametrize("filename, epoch",
[
("date_1900.xlsx", CALENDAR_WINDOWS_1900),
("date_1904.xlsx", CALENDAR_MAC_1904),
]
)
def test_read_win_base_date(datadir, filename, epoch):
from .. workbook import read_excel_base_date
datadir.chdir()
archive = ZipFile(filename)
base_date = read_excel_base_date(archive)
assert base_date == epoch
def test_missing_ids(datadir, DummyArchive):
datadir.chdir()
with open("workbook_missing_ids.xml") as src:
xml = src.read()
archive = DummyArchive
archive.writestr("xl/workbook.xml", xml)
from ..workbook import read_sheets
sheets = read_sheets(archive)
assert list(sheets) == [
{'sheetId': '1', 'id': 'rId1', 'name': '4CASTING RAP'},
{'sheetId': '11', 'id': 'rId2', 'name': '4CAST SLOPS'},
{'sheetId': '20', 'id': 'rId3', 'name': 'Chart4'},
{'sheetId': '18', 'id': 'rId4', 'name': 'Chart3'},
{'sheetId': '17', 'id': 'rId5', 'name': 'Chart2'},
{'sheetId': '16', 'id': 'rId6', 'name': 'Chart1'},
{'sheetId': '21', 'id': 'rId7', 'name': 'Sheet1'}
]
|