28
28
from pandas .util .testing import ensure_clean , makeCustomDataframe as mkdf
29
29
30
30
31
- def _skip_if_no_xlwt ():
32
- try :
33
- import xlwt # NOQA
34
- except ImportError :
35
- pytest .skip ('xlwt not installed, skipping' )
36
-
37
-
38
- def _skip_if_no_openpyxl ():
39
- try :
40
- import openpyxl # NOQA
41
- except ImportError :
42
- pytest .skip ('openpyxl not installed, skipping' )
43
-
44
-
45
- def _skip_if_no_xlsxwriter ():
46
- try :
47
- import xlsxwriter # NOQA
48
- except ImportError :
49
- pytest .skip ('xlsxwriter not installed, skipping' )
50
-
51
-
52
31
_seriesd = tm .getSeriesData ()
53
32
_tsd = tm .getTimeSeriesData ()
54
33
_frame = DataFrame (_seriesd )[:10 ]
@@ -2096,13 +2075,13 @@ def test_to_excel_styleconverter(self, merge_cells, ext, engine):
2096
2075
(None , '.xlsx' , 'xlsxwriter' )])
2097
2076
class TestXlsxWriterTests (_WriterBase ):
2098
2077
2078
+ @td .skip_if_no ('openpyxl' )
2099
2079
def test_column_format (self , merge_cells , ext , engine ):
2100
2080
# Test that column formats are applied to cells. Test for issue #9167.
2101
2081
# Applicable to xlsxwriter only.
2102
2082
with warnings .catch_warnings ():
2103
2083
# Ignore the openpyxl lxml warning.
2104
2084
warnings .simplefilter ("ignore" )
2105
- _skip_if_no_openpyxl ()
2106
2085
import openpyxl
2107
2086
2108
2087
with ensure_clean (ext ) as path :
@@ -2144,25 +2123,26 @@ def test_column_format(self, merge_cells, ext, engine):
2144
2123
2145
2124
class TestExcelWriterEngineTests (object ):
2146
2125
2147
- def test_ExcelWriter_dispatch (self ):
2148
- with tm .assert_raises_regex (ValueError , 'No engine' ):
2149
- ExcelWriter ('nothing' )
2150
-
2151
- try :
2152
- import xlsxwriter # noqa
2153
- writer_klass = _XlsxWriter
2154
- except ImportError :
2155
- _skip_if_no_openpyxl ()
2156
- writer_klass = _OpenpyxlWriter
2157
-
2158
- with ensure_clean ('.xlsx' ) as path :
2126
+ @pytest .mark .parametrize ('klass,ext' , [
2127
+ pytest .param (_XlsxWriter , '.xlsx' , marks = pytest .mark .skipif (
2128
+ not td .safe_import ('xlsxwriter' ), reason = 'No xlsxwriter' )),
2129
+ pytest .param (_OpenpyxlWriter , '.xlsx' , marks = pytest .mark .skipif (
2130
+ not td .safe_import ('openpyxl' ), reason = 'No openpyxl' )),
2131
+ pytest .param (_XlwtWriter , '.xls' , marks = pytest .mark .skipif (
2132
+ not td .safe_import ('xlwt' ), reason = 'No xlwt' ))
2133
+ ])
2134
+ def test_ExcelWriter_dispatch (self , klass , ext ):
2135
+ with ensure_clean (ext ) as path :
2159
2136
writer = ExcelWriter (path )
2160
- assert isinstance (writer , writer_klass )
2137
+ if ext == '.xlsx' and td .safe_import ('xlsxwriter' ):
2138
+ # xlsxwriter has preference over openpyxl if both installed
2139
+ assert isinstance (writer , _XlsxWriter )
2140
+ else :
2141
+ assert isinstance (writer , klass )
2161
2142
2162
- _skip_if_no_xlwt ()
2163
- with ensure_clean ('.xls' ) as path :
2164
- writer = ExcelWriter (path )
2165
- assert isinstance (writer , _XlwtWriter )
2143
+ def test_ExcelWriter_dispatch_raises (self ):
2144
+ with tm .assert_raises_regex (ValueError , 'No engine' ):
2145
+ ExcelWriter ('nothing' )
2166
2146
2167
2147
def test_register_writer (self ):
2168
2148
# some awkward mocking to test out dispatch and such actually works
@@ -2353,11 +2333,11 @@ def custom_converter(css):
2353
2333
assert n_cells == (10 + 1 ) * (3 + 1 )
2354
2334
2355
2335
2336
+ @td .skip_if_no ('openpyxl' )
2356
2337
class TestFSPath (object ):
2357
2338
2358
2339
@pytest .mark .skipif (sys .version_info < (3 , 6 ), reason = 'requires fspath' )
2359
2340
def test_excelfile_fspath (self ):
2360
- _skip_if_no_openpyxl ()
2361
2341
with tm .ensure_clean ('foo.xlsx' ) as path :
2362
2342
df = DataFrame ({"A" : [1 , 2 ]})
2363
2343
df .to_excel (path )
@@ -2368,7 +2348,6 @@ def test_excelfile_fspath(self):
2368
2348
@pytest .mark .skipif (sys .version_info < (3 , 6 ), reason = 'requires fspath' )
2369
2349
# @pytest.mark.xfail
2370
2350
def test_excelwriter_fspath (self ):
2371
- _skip_if_no_openpyxl ()
2372
2351
with tm .ensure_clean ('foo.xlsx' ) as path :
2373
2352
writer = ExcelWriter (path )
2374
2353
assert os .fspath (writer ) == str (path )
0 commit comments