def test_ctor(self, Stylesheet): parser = Stylesheet() xml = tostring(parser.to_tree()) expected = """ <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> <numFmts count="0"></numFmts> <cellStyleXfs count="0"></cellStyleXfs> <cellXfs count="0"></cellXfs> <cellStyles count="0"></cellStyles> </styleSheet> """ diff = compare_xml(xml, expected) assert diff is None, diff
def test_from_complex(self, Stylesheet, datadir): datadir.chdir() with open("complex-styles.xml") as src: xml = src.read() node = fromstring(xml) stylesheet = Stylesheet.from_tree(node) assert stylesheet.numFmts.numFmt == []
def test_split_named_styles(self, Stylesheet): from openpyxl25.workbook.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
def test_named_styles(self, datadir, Stylesheet): from openpyxl25.styles.named_styles import NamedStyle from openpyxl25.styles.fonts import DEFAULT_FONT from openpyxl25.styles.fills import DEFAULT_EMPTY_FILL from openpyxl25.styles.borders import Border datadir.chdir() with open("complex-styles.xml") as src: xml = src.read() node = fromstring(xml) stylesheet = Stylesheet.from_tree(node) followed = stylesheet.named_styles['Followed Hyperlink'] assert followed.xfId == 2 assert followed.name == "Followed Hyperlink" assert followed.font == stylesheet.fonts[2] assert followed.fill == DEFAULT_EMPTY_FILL assert followed.border == Border() link = stylesheet.named_styles['Hyperlink'] assert link.name == "Hyperlink" assert link.font == stylesheet.fonts[1] assert link.fill == DEFAULT_EMPTY_FILL assert link.border == Border() normal = stylesheet.named_styles['Normal'] assert normal.name == "Normal" assert normal.font == stylesheet.fonts[0] assert normal.fill == DEFAULT_EMPTY_FILL assert normal.border == Border()
def test_merge_named_styles(self, Stylesheet, datadir): datadir.chdir() with open("complex-styles.xml") as src: xml = src.read() node = fromstring(xml) stylesheet = Stylesheet.from_tree(node) named_styles = stylesheet._merge_named_styles() assert len(named_styles) == 3
def test_read_cell_style(self, datadir, Stylesheet): datadir.chdir() with open("empty-workbook-styles.xml") as src: xml = src.read() node = fromstring(xml) stylesheet = Stylesheet.from_tree(node) styles = stylesheet.cell_styles assert len(styles) == 2 assert styles[1] == StyleArray([0,0,0,9,0,0,0,0,1])
def test_remove_duplicate_number_formats(self, Stylesheet, datadir): datadir.chdir() with open("builtins_as_custom_number_formats.xml", "rb") as src: xml = src.read() node = fromstring(xml) stylesheet = Stylesheet.from_tree(node) assert stylesheet.number_formats == ['dd\\/mm']
def test_rgb_colors(self, Stylesheet, datadir): datadir.chdir() with open("rgb_colors.xml") as src: xml = src.read() node = fromstring(xml) stylesheet = Stylesheet.from_tree(node) assert len(stylesheet.colors.index) == 64 assert stylesheet.colors.index[0] == "00000000" assert stylesheet.colors.index[-1] == "00333333"
def test_read_xf_no_number_format(self, datadir, Stylesheet): datadir.chdir() with open("no_number_format.xml") as src: xml = src.read() node = fromstring(xml) stylesheet = Stylesheet.from_tree(node) styles = stylesheet.cell_styles assert len(styles) == 3 assert styles[1] == StyleArray([1,0,1,0,0,0,0,0,0]) assert styles[2] == StyleArray([0,0,0,14,0,0,0,0,0])
def test_none_values(self, datadir, Stylesheet): datadir.chdir() with open("none_value_styles.xml") as src: xml = src.read() node = fromstring(xml) stylesheet = Stylesheet.from_tree(node) fonts = stylesheet.fonts assert fonts[0].scheme is None assert fonts[0].vertAlign is None assert fonts[1].u is None
def test_unprotected_cell(self, Stylesheet, datadir): datadir.chdir() with open ("worksheet_unprotected_style.xml") as src: xml = src.read() node = fromstring(xml) stylesheet = Stylesheet.from_tree(node) styles = stylesheet.cell_styles assert len(styles) == 3 # default is cells are locked assert styles[1] == StyleArray([4,0,0,0,0,0,0,0,0]) assert styles[2] == StyleArray([3,0,0,0,1,0,0,0,0])
def test_custom_number_formats(self, Stylesheet, datadir): datadir.chdir() with open("styles_number_formats.xml", "rb") as src: xml = src.read() node = fromstring(xml) stylesheet = Stylesheet.from_tree(node) assert set(stylesheet.number_formats) == set([ '_ * #,##0.00_ ;_ * \-#,##0.00_ ;_ * "-"??_ ;_ @_ ', "#,##0.00_ ", "yyyy/m/d;@", "0.00000_ " ])
def test_alignment(self, datadir, Stylesheet): datadir.chdir() with open("alignment_styles.xml") as src: xml = src.read() node = fromstring(xml) stylesheet = Stylesheet.from_tree(node) styles = stylesheet.cell_styles assert len(styles) == 3 assert styles[2] == StyleArray([0,0,0,0,0,2,0,0,0]) from openpyxl25.styles.alignment import Alignment assert stylesheet.alignments == [ Alignment(), Alignment(textRotation=180), Alignment(vertical='top', textRotation=255), ]
def test_assign_number_formats(self, Stylesheet): node = fromstring(""" <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> <numFmts count="1"> <numFmt numFmtId="43" formatCode='_ * #,##0.00_ ;_ * \-#,##0.00_ ;_ * "-"??_ ;_ @_ ' /> </numFmts> <cellXfs count="0"> <xf numFmtId="43" fontId="2" fillId="0" borderId="0" applyFont="0" applyFill="0" applyBorder="0" applyAlignment="0" applyProtection="0"> <alignment vertical="center"/> </xf> </cellXfs> </styleSheet> """) stylesheet = Stylesheet.from_tree(node) styles = stylesheet.cell_styles assert styles[0] == StyleArray([2, 0, 0, 164, 0, 1, 0, 0, 0])