|
1 | 1 | import pytest
|
2 | 2 |
|
3 |
| -import pandas.util._test_decorators as td |
4 |
| - |
5 | 3 | from pandas import DataFrame
|
6 | 4 | from pandas.util.testing import ensure_clean
|
7 | 5 |
|
8 | 6 | from pandas.io.excel import ExcelWriter, _OpenpyxlWriter
|
9 | 7 |
|
10 |
| - |
11 |
| -@td.skip_if_no('openpyxl') |
12 |
| -@pytest.mark.parametrize("ext", ['.xlsx']) |
13 |
| -class TestOpenpyxlTests: |
14 |
| - |
15 |
| - def test_to_excel_styleconverter(self, ext): |
16 |
| - from openpyxl import styles |
17 |
| - |
18 |
| - hstyle = { |
19 |
| - "font": { |
20 |
| - "color": '00FF0000', |
21 |
| - "bold": True, |
22 |
| - }, |
23 |
| - "borders": { |
24 |
| - "top": "thin", |
25 |
| - "right": "thin", |
26 |
| - "bottom": "thin", |
27 |
| - "left": "thin", |
28 |
| - }, |
29 |
| - "alignment": { |
30 |
| - "horizontal": "center", |
31 |
| - "vertical": "top", |
32 |
| - }, |
33 |
| - "fill": { |
34 |
| - "patternType": 'solid', |
35 |
| - 'fgColor': { |
36 |
| - 'rgb': '006666FF', |
37 |
| - 'tint': 0.3, |
38 |
| - }, |
39 |
| - }, |
40 |
| - "number_format": { |
41 |
| - "format_code": "0.00" |
42 |
| - }, |
43 |
| - "protection": { |
44 |
| - "locked": True, |
45 |
| - "hidden": False, |
| 8 | +openpyxl = pytest.importorskip("openpyxl") |
| 9 | + |
| 10 | +pytestmark = pytest.mark.parametrize("ext", ['.xlsx']) |
| 11 | + |
| 12 | + |
| 13 | +def test_to_excel_styleconverter(ext): |
| 14 | + from openpyxl import styles |
| 15 | + |
| 16 | + hstyle = { |
| 17 | + "font": { |
| 18 | + "color": '00FF0000', |
| 19 | + "bold": True, |
| 20 | + }, |
| 21 | + "borders": { |
| 22 | + "top": "thin", |
| 23 | + "right": "thin", |
| 24 | + "bottom": "thin", |
| 25 | + "left": "thin", |
| 26 | + }, |
| 27 | + "alignment": { |
| 28 | + "horizontal": "center", |
| 29 | + "vertical": "top", |
| 30 | + }, |
| 31 | + "fill": { |
| 32 | + "patternType": 'solid', |
| 33 | + 'fgColor': { |
| 34 | + 'rgb': '006666FF', |
| 35 | + 'tint': 0.3, |
46 | 36 | },
|
47 |
| - } |
48 |
| - |
49 |
| - font_color = styles.Color('00FF0000') |
50 |
| - font = styles.Font(bold=True, color=font_color) |
51 |
| - side = styles.Side(style=styles.borders.BORDER_THIN) |
52 |
| - border = styles.Border(top=side, right=side, bottom=side, left=side) |
53 |
| - alignment = styles.Alignment(horizontal='center', vertical='top') |
54 |
| - fill_color = styles.Color(rgb='006666FF', tint=0.3) |
55 |
| - fill = styles.PatternFill(patternType='solid', fgColor=fill_color) |
56 |
| - |
57 |
| - number_format = '0.00' |
58 |
| - |
59 |
| - protection = styles.Protection(locked=True, hidden=False) |
60 |
| - |
61 |
| - kw = _OpenpyxlWriter._convert_to_style_kwargs(hstyle) |
62 |
| - assert kw['font'] == font |
63 |
| - assert kw['border'] == border |
64 |
| - assert kw['alignment'] == alignment |
65 |
| - assert kw['fill'] == fill |
66 |
| - assert kw['number_format'] == number_format |
67 |
| - assert kw['protection'] == protection |
68 |
| - |
69 |
| - def test_write_cells_merge_styled(self, ext): |
70 |
| - from pandas.io.formats.excel import ExcelCell |
71 |
| - |
72 |
| - sheet_name = 'merge_styled' |
73 |
| - |
74 |
| - sty_b1 = {'font': {'color': '00FF0000'}} |
75 |
| - sty_a2 = {'font': {'color': '0000FF00'}} |
76 |
| - |
77 |
| - initial_cells = [ |
78 |
| - ExcelCell(col=1, row=0, val=42, style=sty_b1), |
79 |
| - ExcelCell(col=0, row=1, val=99, style=sty_a2), |
80 |
| - ] |
81 |
| - |
82 |
| - sty_merged = {'font': {'color': '000000FF', 'bold': True}} |
83 |
| - sty_kwargs = _OpenpyxlWriter._convert_to_style_kwargs(sty_merged) |
84 |
| - openpyxl_sty_merged = sty_kwargs['font'] |
85 |
| - merge_cells = [ |
86 |
| - ExcelCell(col=0, row=0, val='pandas', |
87 |
| - mergestart=1, mergeend=1, style=sty_merged), |
88 |
| - ] |
89 |
| - |
90 |
| - with ensure_clean(ext) as path: |
91 |
| - writer = _OpenpyxlWriter(path) |
92 |
| - writer.write_cells(initial_cells, sheet_name=sheet_name) |
93 |
| - writer.write_cells(merge_cells, sheet_name=sheet_name) |
94 |
| - |
95 |
| - wks = writer.sheets[sheet_name] |
96 |
| - xcell_b1 = wks['B1'] |
97 |
| - xcell_a2 = wks['A2'] |
98 |
| - assert xcell_b1.font == openpyxl_sty_merged |
99 |
| - assert xcell_a2.font == openpyxl_sty_merged |
100 |
| - |
101 |
| - @pytest.mark.parametrize("mode,expected", [ |
102 |
| - ('w', ['baz']), ('a', ['foo', 'bar', 'baz'])]) |
103 |
| - def test_write_append_mode(self, ext, mode, expected): |
104 |
| - import openpyxl |
105 |
| - df = DataFrame([1], columns=['baz']) |
106 |
| - |
107 |
| - with ensure_clean(ext) as f: |
108 |
| - wb = openpyxl.Workbook() |
109 |
| - wb.worksheets[0].title = 'foo' |
110 |
| - wb.worksheets[0]['A1'].value = 'foo' |
111 |
| - wb.create_sheet('bar') |
112 |
| - wb.worksheets[1]['A1'].value = 'bar' |
113 |
| - wb.save(f) |
114 |
| - |
115 |
| - writer = ExcelWriter(f, engine='openpyxl', mode=mode) |
116 |
| - df.to_excel(writer, sheet_name='baz', index=False) |
117 |
| - writer.save() |
118 |
| - |
119 |
| - wb2 = openpyxl.load_workbook(f) |
120 |
| - result = [sheet.title for sheet in wb2.worksheets] |
121 |
| - assert result == expected |
122 |
| - |
123 |
| - for index, cell_value in enumerate(expected): |
124 |
| - assert wb2.worksheets[index]['A1'].value == cell_value |
| 37 | + }, |
| 38 | + "number_format": { |
| 39 | + "format_code": "0.00" |
| 40 | + }, |
| 41 | + "protection": { |
| 42 | + "locked": True, |
| 43 | + "hidden": False, |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + font_color = styles.Color('00FF0000') |
| 48 | + font = styles.Font(bold=True, color=font_color) |
| 49 | + side = styles.Side(style=styles.borders.BORDER_THIN) |
| 50 | + border = styles.Border(top=side, right=side, bottom=side, left=side) |
| 51 | + alignment = styles.Alignment(horizontal='center', vertical='top') |
| 52 | + fill_color = styles.Color(rgb='006666FF', tint=0.3) |
| 53 | + fill = styles.PatternFill(patternType='solid', fgColor=fill_color) |
| 54 | + |
| 55 | + number_format = '0.00' |
| 56 | + |
| 57 | + protection = styles.Protection(locked=True, hidden=False) |
| 58 | + |
| 59 | + kw = _OpenpyxlWriter._convert_to_style_kwargs(hstyle) |
| 60 | + assert kw['font'] == font |
| 61 | + assert kw['border'] == border |
| 62 | + assert kw['alignment'] == alignment |
| 63 | + assert kw['fill'] == fill |
| 64 | + assert kw['number_format'] == number_format |
| 65 | + assert kw['protection'] == protection |
| 66 | + |
| 67 | + |
| 68 | +def test_write_cells_merge_styled(ext): |
| 69 | + from pandas.io.formats.excel import ExcelCell |
| 70 | + |
| 71 | + sheet_name = 'merge_styled' |
| 72 | + |
| 73 | + sty_b1 = {'font': {'color': '00FF0000'}} |
| 74 | + sty_a2 = {'font': {'color': '0000FF00'}} |
| 75 | + |
| 76 | + initial_cells = [ |
| 77 | + ExcelCell(col=1, row=0, val=42, style=sty_b1), |
| 78 | + ExcelCell(col=0, row=1, val=99, style=sty_a2), |
| 79 | + ] |
| 80 | + |
| 81 | + sty_merged = {'font': {'color': '000000FF', 'bold': True}} |
| 82 | + sty_kwargs = _OpenpyxlWriter._convert_to_style_kwargs(sty_merged) |
| 83 | + openpyxl_sty_merged = sty_kwargs['font'] |
| 84 | + merge_cells = [ |
| 85 | + ExcelCell(col=0, row=0, val='pandas', |
| 86 | + mergestart=1, mergeend=1, style=sty_merged), |
| 87 | + ] |
| 88 | + |
| 89 | + with ensure_clean(ext) as path: |
| 90 | + writer = _OpenpyxlWriter(path) |
| 91 | + writer.write_cells(initial_cells, sheet_name=sheet_name) |
| 92 | + writer.write_cells(merge_cells, sheet_name=sheet_name) |
| 93 | + |
| 94 | + wks = writer.sheets[sheet_name] |
| 95 | + xcell_b1 = wks['B1'] |
| 96 | + xcell_a2 = wks['A2'] |
| 97 | + assert xcell_b1.font == openpyxl_sty_merged |
| 98 | + assert xcell_a2.font == openpyxl_sty_merged |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.parametrize("mode,expected", [ |
| 102 | + ('w', ['baz']), ('a', ['foo', 'bar', 'baz'])]) |
| 103 | +def test_write_append_mode(ext, mode, expected): |
| 104 | + df = DataFrame([1], columns=['baz']) |
| 105 | + |
| 106 | + with ensure_clean(ext) as f: |
| 107 | + wb = openpyxl.Workbook() |
| 108 | + wb.worksheets[0].title = 'foo' |
| 109 | + wb.worksheets[0]['A1'].value = 'foo' |
| 110 | + wb.create_sheet('bar') |
| 111 | + wb.worksheets[1]['A1'].value = 'bar' |
| 112 | + wb.save(f) |
| 113 | + |
| 114 | + writer = ExcelWriter(f, engine='openpyxl', mode=mode) |
| 115 | + df.to_excel(writer, sheet_name='baz', index=False) |
| 116 | + writer.save() |
| 117 | + |
| 118 | + wb2 = openpyxl.load_workbook(f) |
| 119 | + result = [sheet.title for sheet in wb2.worksheets] |
| 120 | + assert result == expected |
| 121 | + |
| 122 | + for index, cell_value in enumerate(expected): |
| 123 | + assert wb2.worksheets[index]['A1'].value == cell_value |
0 commit comments