1
+ import contextlib
1
2
from pathlib import Path
2
3
import re
3
4
@@ -159,12 +160,12 @@ def test_write_append_mode(ext, mode, expected):
159
160
with ExcelWriter (f , engine = "openpyxl" , mode = mode ) as writer :
160
161
df .to_excel (writer , sheet_name = "baz" , index = False )
161
162
162
- wb2 = openpyxl .load_workbook (f )
163
- result = [sheet .title for sheet in wb2 .worksheets ]
164
- assert result == expected
163
+ with contextlib . closing ( openpyxl .load_workbook (f )) as wb2 :
164
+ result = [sheet .title for sheet in wb2 .worksheets ]
165
+ assert result == expected
165
166
166
- for index , cell_value in enumerate (expected ):
167
- assert wb2 .worksheets [index ]["A1" ].value == cell_value
167
+ for index , cell_value in enumerate (expected ):
168
+ assert wb2 .worksheets [index ]["A1" ].value == cell_value
168
169
169
170
170
171
@pytest .mark .parametrize (
@@ -187,15 +188,14 @@ def test_if_sheet_exists_append_modes(ext, if_sheet_exists, num_sheets, expected
187
188
) as writer :
188
189
df2 .to_excel (writer , sheet_name = "foo" , index = False )
189
190
190
- wb = openpyxl .load_workbook (f )
191
- assert len (wb .sheetnames ) == num_sheets
192
- assert wb .sheetnames [0 ] == "foo"
193
- result = pd .read_excel (wb , "foo" , engine = "openpyxl" )
194
- assert list (result ["fruit" ]) == expected
195
- if len (wb .sheetnames ) == 2 :
196
- result = pd .read_excel (wb , wb .sheetnames [1 ], engine = "openpyxl" )
197
- tm .assert_frame_equal (result , df2 )
198
- wb .close ()
191
+ with contextlib .closing (openpyxl .load_workbook (f )) as wb :
192
+ assert len (wb .sheetnames ) == num_sheets
193
+ assert wb .sheetnames [0 ] == "foo"
194
+ result = pd .read_excel (wb , "foo" , engine = "openpyxl" )
195
+ assert list (result ["fruit" ]) == expected
196
+ if len (wb .sheetnames ) == 2 :
197
+ result = pd .read_excel (wb , wb .sheetnames [1 ], engine = "openpyxl" )
198
+ tm .assert_frame_equal (result , df2 )
199
199
200
200
201
201
@pytest .mark .parametrize (
@@ -279,9 +279,10 @@ def test_to_excel_with_openpyxl_engine(ext):
279
279
def test_read_workbook (datapath , ext , read_only ):
280
280
# GH 39528
281
281
filename = datapath ("io" , "data" , "excel" , "test1" + ext )
282
- wb = openpyxl .load_workbook (filename , read_only = read_only )
283
- result = pd .read_excel (wb , engine = "openpyxl" )
284
- wb .close ()
282
+ with contextlib .closing (
283
+ openpyxl .load_workbook (filename , read_only = read_only )
284
+ ) as wb :
285
+ result = pd .read_excel (wb , engine = "openpyxl" )
285
286
expected = pd .read_excel (filename )
286
287
tm .assert_frame_equal (result , expected )
287
288
@@ -313,9 +314,10 @@ def test_read_with_bad_dimension(
313
314
if read_only is None :
314
315
result = pd .read_excel (path , header = header )
315
316
else :
316
- wb = openpyxl .load_workbook (path , read_only = read_only )
317
- result = pd .read_excel (wb , engine = "openpyxl" , header = header )
318
- wb .close ()
317
+ with contextlib .closing (
318
+ openpyxl .load_workbook (path , read_only = read_only )
319
+ ) as wb :
320
+ result = pd .read_excel (wb , engine = "openpyxl" , header = header )
319
321
expected = DataFrame (expected_data )
320
322
tm .assert_frame_equal (result , expected )
321
323
@@ -349,9 +351,10 @@ def test_read_with_empty_trailing_rows(datapath, ext, read_only, request):
349
351
if read_only is None :
350
352
result = pd .read_excel (path )
351
353
else :
352
- wb = openpyxl .load_workbook (path , read_only = read_only )
353
- result = pd .read_excel (wb , engine = "openpyxl" )
354
- wb .close ()
354
+ with contextlib .closing (
355
+ openpyxl .load_workbook (path , read_only = read_only )
356
+ ) as wb :
357
+ result = pd .read_excel (wb , engine = "openpyxl" )
355
358
expected = DataFrame (
356
359
{
357
360
"Title" : [np .nan , "A" , 1 , 2 , 3 ],
@@ -370,8 +373,9 @@ def test_read_empty_with_blank_row(datapath, ext, read_only):
370
373
if read_only is None :
371
374
result = pd .read_excel (path )
372
375
else :
373
- wb = openpyxl .load_workbook (path , read_only = read_only )
374
- result = pd .read_excel (wb , engine = "openpyxl" )
375
- wb .close ()
376
+ with contextlib .closing (
377
+ openpyxl .load_workbook (path , read_only = read_only )
378
+ ) as wb :
379
+ result = pd .read_excel (wb , engine = "openpyxl" )
376
380
expected = DataFrame ()
377
381
tm .assert_frame_equal (result , expected )
0 commit comments