1
- from typing import List
1
+ from typing import Any , Dict , List , Optional
2
2
3
3
import numpy as np
4
4
@@ -22,53 +22,22 @@ def __init__(self, path, engine=None, mode="w", **engine_kwargs):
22
22
if self .mode == "a" : # Load from existing workbook
23
23
from openpyxl import load_workbook
24
24
25
- book = load_workbook (self .path )
26
- self .book = book
25
+ self .book = load_workbook (self .path )
27
26
else :
28
27
# Create workbook object with default optimized_write=True.
29
28
self .book = Workbook ()
30
29
31
30
if self .book .worksheets :
32
- try :
33
- self .book .remove (self .book .worksheets [0 ])
34
- except AttributeError :
35
-
36
- # compat - for openpyxl <= 2.4
37
- self .book .remove_sheet (self .book .worksheets [0 ])
31
+ self .book .remove (self .book .worksheets [0 ])
38
32
39
33
def save (self ):
40
34
"""
41
35
Save workbook to disk.
42
36
"""
43
- return self .book .save (self .path )
37
+ self .book .save (self .path )
44
38
45
39
@classmethod
46
- def _convert_to_style (cls , style_dict ):
47
- """
48
- Converts a style_dict to an openpyxl style object.
49
-
50
- Parameters
51
- ----------
52
- style_dict : style dictionary to convert
53
- """
54
- from openpyxl .style import Style
55
-
56
- xls_style = Style ()
57
- for key , value in style_dict .items ():
58
- for nk , nv in value .items ():
59
- if key == "borders" :
60
- (
61
- xls_style .borders .__getattribute__ (nk ).__setattr__ (
62
- "border_style" , nv
63
- )
64
- )
65
- else :
66
- xls_style .__getattribute__ (key ).__setattr__ (nk , nv )
67
-
68
- return xls_style
69
-
70
- @classmethod
71
- def _convert_to_style_kwargs (cls , style_dict ):
40
+ def _convert_to_style_kwargs (cls , style_dict : dict ) -> Dict [str , Any ]:
72
41
"""
73
42
Convert a style_dict to a set of kwargs suitable for initializing
74
43
or updating-on-copy an openpyxl v2 style object.
@@ -93,7 +62,7 @@ def _convert_to_style_kwargs(cls, style_dict):
93
62
"""
94
63
_style_key_map = {"borders" : "border" }
95
64
96
- style_kwargs = {}
65
+ style_kwargs : Dict [ str , Any ] = {}
97
66
for k , v in style_dict .items ():
98
67
if k in _style_key_map :
99
68
k = _style_key_map [k ]
@@ -404,7 +373,7 @@ def write_cells(
404
373
# Write the frame cells using openpyxl.
405
374
sheet_name = self ._get_sheet_name (sheet_name )
406
375
407
- _style_cache = {}
376
+ _style_cache : Dict [ str , Dict [ str , Any ]] = {}
408
377
409
378
if sheet_name in self .sheets :
410
379
wks = self .sheets [sheet_name ]
@@ -426,7 +395,7 @@ def write_cells(
426
395
if fmt :
427
396
xcell .number_format = fmt
428
397
429
- style_kwargs = {}
398
+ style_kwargs : Optional [ Dict [ str , Any ]] = {}
430
399
if cell .style :
431
400
key = str (cell .style )
432
401
style_kwargs = _style_cache .get (key )
@@ -515,16 +484,17 @@ def get_sheet_by_index(self, index: int):
515
484
516
485
def _convert_cell (self , cell , convert_float : bool ) -> Scalar :
517
486
518
- # TODO: replace with openpyxl constants
487
+ from openpyxl .cell .cell import TYPE_BOOL , TYPE_ERROR , TYPE_NUMERIC
488
+
519
489
if cell .is_date :
520
490
return cell .value
521
- elif cell .data_type == "e" :
491
+ elif cell .data_type == TYPE_ERROR :
522
492
return np .nan
523
- elif cell .data_type == "b" :
493
+ elif cell .data_type == TYPE_BOOL :
524
494
return bool (cell .value )
525
495
elif cell .value is None :
526
496
return "" # compat with xlrd
527
- elif cell .data_type == "n" :
497
+ elif cell .data_type == TYPE_NUMERIC :
528
498
# GH5394
529
499
if convert_float :
530
500
val = int (cell .value )
0 commit comments