1
- from typing import List
1
+ from typing import TYPE_CHECKING , Dict , List , Optional
2
2
3
3
import numpy as np
4
4
8
8
from pandas .io .excel ._base import ExcelWriter , _BaseExcelReader
9
9
from pandas .io .excel ._util import _validate_freeze_panes
10
10
11
+ if TYPE_CHECKING :
12
+ from openpyxl .descriptors .serialisable import Serialisable
13
+
11
14
12
15
class _OpenpyxlWriter (ExcelWriter ):
13
16
engine = "openpyxl"
@@ -22,53 +25,22 @@ def __init__(self, path, engine=None, mode="w", **engine_kwargs):
22
25
if self .mode == "a" : # Load from existing workbook
23
26
from openpyxl import load_workbook
24
27
25
- book = load_workbook (self .path )
26
- self .book = book
28
+ self .book = load_workbook (self .path )
27
29
else :
28
30
# Create workbook object with default optimized_write=True.
29
31
self .book = Workbook ()
30
32
31
33
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 ])
34
+ self .book .remove (self .book .worksheets [0 ])
38
35
39
36
def save (self ):
40
37
"""
41
38
Save workbook to disk.
42
39
"""
43
- return self .book .save (self .path )
44
-
45
- @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
40
+ self .book .save (self .path )
69
41
70
42
@classmethod
71
- def _convert_to_style_kwargs (cls , style_dict ) :
43
+ def _convert_to_style_kwargs (cls , style_dict : dict ) -> Dict [ str , "Serialisable" ] :
72
44
"""
73
45
Convert a style_dict to a set of kwargs suitable for initializing
74
46
or updating-on-copy an openpyxl v2 style object.
@@ -93,7 +65,7 @@ def _convert_to_style_kwargs(cls, style_dict):
93
65
"""
94
66
_style_key_map = {"borders" : "border" }
95
67
96
- style_kwargs = {}
68
+ style_kwargs : Dict [ str , Serialisable ] = {}
97
69
for k , v in style_dict .items ():
98
70
if k in _style_key_map :
99
71
k = _style_key_map [k ]
@@ -404,7 +376,7 @@ def write_cells(
404
376
# Write the frame cells using openpyxl.
405
377
sheet_name = self ._get_sheet_name (sheet_name )
406
378
407
- _style_cache = {}
379
+ _style_cache : Dict [ str , Dict [ str , Serialisable ]] = {}
408
380
409
381
if sheet_name in self .sheets :
410
382
wks = self .sheets [sheet_name ]
@@ -426,7 +398,7 @@ def write_cells(
426
398
if fmt :
427
399
xcell .number_format = fmt
428
400
429
- style_kwargs = {}
401
+ style_kwargs : Optional [ Dict [ str , Serialisable ]] = {}
430
402
if cell .style :
431
403
key = str (cell .style )
432
404
style_kwargs = _style_cache .get (key )
@@ -515,16 +487,17 @@ def get_sheet_by_index(self, index: int):
515
487
516
488
def _convert_cell (self , cell , convert_float : bool ) -> Scalar :
517
489
518
- # TODO: replace with openpyxl constants
490
+ from openpyxl .cell .cell import TYPE_BOOL , TYPE_ERROR , TYPE_NUMERIC
491
+
519
492
if cell .is_date :
520
493
return cell .value
521
- elif cell .data_type == "e" :
494
+ elif cell .data_type == TYPE_ERROR :
522
495
return np .nan
523
- elif cell .data_type == "b" :
496
+ elif cell .data_type == TYPE_BOOL :
524
497
return bool (cell .value )
525
498
elif cell .value is None :
526
499
return "" # compat with xlrd
527
- elif cell .data_type == "n" :
500
+ elif cell .data_type == TYPE_NUMERIC :
528
501
# GH5394
529
502
if convert_float :
530
503
val = int (cell .value )
0 commit comments