def __new__( cls, path: Union[FilePathOrBuffer, ExcelWriter], engine=None, date_format=None, datetime_format=None, mode: str = "w", storage_options: StorageOptions = None, engine_kwargs: Optional[Dict] = None, **kwargs, ): if kwargs: if engine_kwargs is not None: raise ValueError("Cannot use both engine_kwargs and **kwargs") warnings.warn( "Use of **kwargs is deprecated, use engine_kwargs instead.", FutureWarning, stacklevel=2, ) # only switch class if generic(ExcelWriter) if cls is ExcelWriter: if engine is None or (isinstance(engine, str) and engine == "auto"): if isinstance(path, str): ext = os.path.splitext(path)[-1][1:] else: ext = "xlsx" try: engine = config.get_option(f"io.excel.{ext}.writer", silent=True) if engine == "auto": engine = get_default_engine(ext, mode="writer") except KeyError as err: raise ValueError( f"No engine for filetype: '{ext}'") from err if engine == "xlwt": xls_config_engine = config.get_option("io.excel.xls.writer", silent=True) # Don't warn a 2nd time if user has changed the default engine for xls if xls_config_engine != "xlwt": warnings.warn( "As the xlwt package is no longer maintained, the xlwt " "engine will be removed in a future version of pandas. " "This is the only engine in pandas that supports writing " "in the xls format. Install openpyxl and write to an xlsx " "file instead. You can set the option io.excel.xls.writer " "to 'xlwt' to silence this warning. While this option is " "deprecated and will also raise a warning, it can " "be globally set and the warning suppressed.", FutureWarning, stacklevel=4, ) cls = get_writer(engine) return object.__new__(cls)
def __init__(self, path_or_buffer, engine=None, storage_options: StorageOptions = None): if engine is not None and engine not in self._engines: raise ValueError(f"Unknown engine: {engine}") # Could be a str, ExcelFile, Book, etc. self.io = path_or_buffer # Always a string self._io = stringify_path(path_or_buffer) # Determine xlrd version if installed if import_optional_dependency("xlrd", errors="ignore") is None: xlrd_version = None else: import xlrd xlrd_version = Version(get_version(xlrd)) ext = None if engine is None: # Only determine ext if it is needed if xlrd_version is not None and isinstance(path_or_buffer, xlrd.Book): ext = "xls" else: ext = inspect_excel_format(content_or_path=path_or_buffer, storage_options=storage_options) if ext is None: raise ValueError( "Excel file format cannot be determined, you must specify " "an engine manually.") engine = config.get_option(f"io.excel.{ext}.reader", silent=True) if engine == "auto": engine = get_default_engine(ext, mode="reader") if engine == "xlrd" and xlrd_version is not None: if ext is None: # Need ext to determine ext in order to raise/warn if isinstance(path_or_buffer, xlrd.Book): ext = "xls" else: ext = inspect_excel_format(path_or_buffer, storage_options=storage_options) # Pass through if ext is None, otherwise check if ext valid for xlrd if ext and ext != "xls" and xlrd_version >= Version("2"): raise ValueError( f"Your version of xlrd is {xlrd_version}. In xlrd >= 2.0, " f"only the xls format is supported. Install openpyxl instead." ) elif ext and ext != "xls": stacklevel = find_stack_level() warnings.warn( f"Your version of xlrd is {xlrd_version}. In xlrd >= 2.0, " f"only the xls format is supported. Install " f"openpyxl instead.", FutureWarning, stacklevel=stacklevel, ) self.engine = engine self.storage_options = storage_options self._reader = self._engines[engine](self._io, storage_options=storage_options)
def __init__(self, path_or_buffer, engine=None, storage_options: StorageOptions = None): if engine is not None and engine not in self._engines: raise ValueError(f"Unknown engine: {engine}") # Could be a str, ExcelFile, Book, etc. self.io = path_or_buffer # Always a string self._io = stringify_path(path_or_buffer) # Determine xlrd version if installed if (import_optional_dependency( "xlrd", raise_on_missing=False, on_version="ignore") is None): xlrd_version = None else: import xlrd xlrd_version = LooseVersion(xlrd.__version__) if xlrd_version is not None and isinstance(path_or_buffer, xlrd.Book): ext = "xls" else: ext = inspect_excel_format(content=path_or_buffer, storage_options=storage_options) if engine is None: # ext will always be valid, otherwise inspect_excel_format would raise engine = config.get_option(f"io.excel.{ext}.reader", silent=True) if engine == "auto": engine = get_default_engine(ext, mode="reader") if engine == "xlrd" and ext != "xls" and xlrd_version is not None: if xlrd_version >= "2": raise ValueError( f"Your version of xlrd is {xlrd_version}. In xlrd >= 2.0, " f"only the xls format is supported. Install openpyxl instead." ) else: caller = inspect.stack()[1] if (caller.filename.endswith( os.path.join("pandas", "io", "excel", "_base.py")) and caller.function == "read_excel"): stacklevel = 4 else: stacklevel = 2 warnings.warn( f"Your version of xlrd is {xlrd_version}. In xlrd >= 2.0, " f"only the xls format is supported. As a result, the " f"openpyxl engine will be used if it is installed and the " f"engine argument is not specified. Install " f"openpyxl instead.", FutureWarning, stacklevel=stacklevel, ) self.engine = engine self.storage_options = storage_options self._reader = self._engines[engine](self._io, storage_options=storage_options)