2828from pandas .util .testing import ensure_clean , makeCustomDataframe as mkdf
2929
3030
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-
5231_seriesd = tm .getSeriesData ()
5332_tsd = tm .getTimeSeriesData ()
5433_frame = DataFrame (_seriesd )[:10 ]
@@ -2096,13 +2075,13 @@ def test_to_excel_styleconverter(self, merge_cells, ext, engine):
20962075 (None , '.xlsx' , 'xlsxwriter' )])
20972076class TestXlsxWriterTests (_WriterBase ):
20982077
2078+ @td .skip_if_no ('openpyxl' )
20992079 def test_column_format (self , merge_cells , ext , engine ):
21002080 # Test that column formats are applied to cells. Test for issue #9167.
21012081 # Applicable to xlsxwriter only.
21022082 with warnings .catch_warnings ():
21032083 # Ignore the openpyxl lxml warning.
21042084 warnings .simplefilter ("ignore" )
2105- _skip_if_no_openpyxl ()
21062085 import openpyxl
21072086
21082087 with ensure_clean (ext ) as path :
@@ -2144,25 +2123,26 @@ def test_column_format(self, merge_cells, ext, engine):
21442123
21452124class TestExcelWriterEngineTests (object ):
21462125
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 :
21592136 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 )
21612142
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' )
21662146
21672147 def test_register_writer (self ):
21682148 # some awkward mocking to test out dispatch and such actually works
@@ -2353,11 +2333,11 @@ def custom_converter(css):
23532333 assert n_cells == (10 + 1 ) * (3 + 1 )
23542334
23552335
2336+ @td .skip_if_no ('openpyxl' )
23562337class TestFSPath (object ):
23572338
23582339 @pytest .mark .skipif (sys .version_info < (3 , 6 ), reason = 'requires fspath' )
23592340 def test_excelfile_fspath (self ):
2360- _skip_if_no_openpyxl ()
23612341 with tm .ensure_clean ('foo.xlsx' ) as path :
23622342 df = DataFrame ({"A" : [1 , 2 ]})
23632343 df .to_excel (path )
@@ -2368,7 +2348,6 @@ def test_excelfile_fspath(self):
23682348 @pytest .mark .skipif (sys .version_info < (3 , 6 ), reason = 'requires fspath' )
23692349 # @pytest.mark.xfail
23702350 def test_excelwriter_fspath (self ):
2371- _skip_if_no_openpyxl ()
23722351 with tm .ensure_clean ('foo.xlsx' ) as path :
23732352 writer = ExcelWriter (path )
23742353 assert os .fspath (writer ) == str (path )
0 commit comments