Commit daa4ea5c authored by CharlieC's avatar CharlieC
Browse files

Use default styles if stylesheet is empty.

Resolves #1673

--HG--
branch : 3.0
parent b01cac6d5237
Pipeline #22097 passed with stage
in 2 minutes and 52 seconds
......@@ -197,23 +197,28 @@ def apply_stylesheet(archive, wb):
node = fromstring(src)
stylesheet = Stylesheet.from_tree(node)
wb._borders = IndexedList(stylesheet.borders)
wb._fonts = IndexedList(stylesheet.fonts)
wb._fills = IndexedList(stylesheet.fills)
wb._differential_styles.styles = stylesheet.dxfs
wb._number_formats = stylesheet.number_formats
wb._protections = stylesheet.protections
wb._alignments = stylesheet.alignments
wb._table_styles = stylesheet.tableStyles
# need to overwrite openpyxl defaults in case workbook has different ones
wb._cell_styles = stylesheet.cell_styles
wb._named_styles = stylesheet.named_styles
wb._date_formats = stylesheet.date_formats
wb._timedelta_formats = stylesheet.timedelta_formats
for ns in wb._named_styles:
ns.bind(wb)
if stylesheet.cell_styles:
wb._borders = IndexedList(stylesheet.borders)
wb._fonts = IndexedList(stylesheet.fonts)
wb._fills = IndexedList(stylesheet.fills)
wb._differential_styles.styles = stylesheet.dxfs
wb._number_formats = stylesheet.number_formats
wb._protections = stylesheet.protections
wb._alignments = stylesheet.alignments
wb._table_styles = stylesheet.tableStyles
# need to overwrite openpyxl defaults in case workbook has different ones
wb._cell_styles = stylesheet.cell_styles
wb._named_styles = stylesheet.named_styles
wb._date_formats = stylesheet.date_formats
wb._timedelta_formats = stylesheet.timedelta_formats
for ns in wb._named_styles:
ns.bind(wb)
else:
warn("Workbook contains no stylesheet, using openpyxl's defaults")
if not wb._named_styles:
normal = styles['Normal']
......
......@@ -247,7 +247,7 @@ class TestStylesheet:
assert stylesheet.cellStyles.cellStyle[-1].xfId < stylesheet.cellStyleXfs.count
def test_no_styles():
def test_no_stylesheet():
from ..stylesheet import apply_stylesheet
wb1 = wb2 = Workbook()
archive = ZipFile(BytesIO(), "a")
......@@ -255,6 +255,16 @@ def test_no_styles():
assert wb1._cell_styles == wb2._cell_styles
assert wb2._named_styles == wb2._named_styles
def test_no_styles(recwarn):
from ..stylesheet import apply_stylesheet
wb = Workbook()
archive = ZipFile(BytesIO(), "a")
xml = b"""<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" />"""
archive.writestr("xl/styles.xml", xml)
apply_stylesheet(archive, wb)
w = recwarn.pop()
assert w.category == UserWarning
def test_write_worksheet(Stylesheet):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment