Skip to content

CLN: inspect_excel_format #39008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import datetime
from distutils.version import LooseVersion
import inspect
from io import BufferedIOBase, BytesIO, RawIOBase
from io import BytesIO
import os
from textwrap import fill
from typing import IO, Any, Dict, Mapping, Optional, Union, cast
from typing import Any, Dict, Mapping, Optional, Union, cast
import warnings
import zipfile

Expand Down Expand Up @@ -906,24 +906,18 @@ def close(self):

@doc(storage_options=_shared_docs["storage_options"])
def inspect_excel_format(
path: Optional[str] = None,
content: Union[None, BufferedIOBase, RawIOBase, bytes] = None,
content_or_path: FilePathOrBuffer,
storage_options: StorageOptions = None,
) -> str:
"""
Inspect the path or content of an excel file and get its format.

At least one of path or content must be not None. If both are not None,
content will take precedence.

Adopted from xlrd: https://github.com/python-excel/xlrd.

Parameters
----------
path : str, optional
Path to file to inspect. May be a URL.
content : file-like object, optional
Content of file to inspect.
content_or_path : str or file-like object
Path to file or content of file to inspect. May be a URL.
{storage_options}

Returns
Expand All @@ -938,12 +932,8 @@ def inspect_excel_format(
BadZipFile
If resulting stream does not have an XLS signature and is not a valid zipfile.
"""
content_or_path: Union[None, str, BufferedIOBase, RawIOBase, IO[bytes]]
if isinstance(content, bytes):
content_or_path = BytesIO(content)
else:
content_or_path = content or path
assert content_or_path is not None
if isinstance(content_or_path, bytes):
content_or_path = BytesIO(content_or_path)

with get_handle(
content_or_path, "rb", storage_options=storage_options, is_text=False
Expand Down Expand Up @@ -1069,7 +1059,7 @@ def __init__(
ext = "xls"
else:
ext = inspect_excel_format(
content=path_or_buffer, storage_options=storage_options
content_or_path=path_or_buffer, storage_options=storage_options
)

if engine is None:
Expand Down