33from io import BufferedIOBase , BytesIO , RawIOBase
44import os
55from textwrap import fill
6- from typing import Any , Mapping , Union
6+ from typing import Any , Dict , Mapping , Union , cast
77
88from pandas ._config import config
99
1010from pandas ._libs .parsers import STR_NA_VALUES
11- from pandas ._typing import StorageOptions
11+ from pandas ._typing import Buffer , FilePathOrBuffer , StorageOptions
1212from pandas .errors import EmptyDataError
1313from pandas .util ._decorators import Appender , deprecate_nonkeyword_arguments
1414
@@ -567,6 +567,12 @@ class ExcelWriter(metaclass=abc.ABCMeta):
567567 File mode to use (write or append). Append does not work with fsspec URLs.
568568
569569 .. versionadded:: 0.24.0
570+ storage_options : dict, optional
571+ Extra options that make sense for a particular storage connection, e.g.
572+ host, port, username, password, etc., if using a URL that will
573+ be parsed by ``fsspec``, e.g., starting "s3://", "gcs://".
574+
575+ .. versionadded:: 1.2.0
570576
571577 Attributes
572578 ----------
@@ -710,11 +716,12 @@ def save(self):
710716
711717 def __init__ (
712718 self ,
713- path ,
719+ path : Union [ FilePathOrBuffer , "ExcelWriter" ] ,
714720 engine = None ,
715721 date_format = None ,
716722 datetime_format = None ,
717- mode = "w" ,
723+ mode : str = "w" ,
724+ storage_options : StorageOptions = None ,
718725 ** engine_kwargs ,
719726 ):
720727 # validate that this engine can handle the extension
@@ -729,10 +736,13 @@ def __init__(
729736 # the excel backend first read the existing file and then write any data to it
730737 mode = mode .replace ("a" , "r+" )
731738
732- self .handles = IOHandles (path , compression = {"copression" : None })
739+ # cast ExcelWriter to avoid adding 'if self.handles is not None'
740+ self .handles = IOHandles (cast (Buffer , path ), compression = {"copression" : None })
733741 if not isinstance (path , ExcelWriter ):
734- self .handles = get_handle (path , mode , is_text = False )
735- self .sheets = {}
742+ self .handles = get_handle (
743+ path , mode , storage_options = storage_options , is_text = False
744+ )
745+ self .sheets : Dict [str , Any ] = {}
736746 self .cur_sheet = None
737747
738748 if date_format is None :
0 commit comments