|
1 | 1 | import abc |
2 | 2 | from collections import OrderedDict |
3 | 3 | from datetime import date, datetime, timedelta |
| 4 | +from io import BytesIO |
4 | 5 | import os |
5 | 6 | from textwrap import fill |
| 7 | +from urllib.request import urlopen |
6 | 8 | import warnings |
7 | 9 |
|
8 | 10 | from pandas._config import config |
9 | 11 |
|
10 | | -import pandas.compat as compat |
11 | 12 | from pandas.errors import EmptyDataError |
12 | 13 | from pandas.util._decorators import Appender, deprecate_kwarg |
13 | 14 |
|
|
16 | 17 |
|
17 | 18 | from pandas.core.frame import DataFrame |
18 | 19 |
|
19 | | -from pandas.io.common import _NA_VALUES, _stringify_path, _validate_header_arg |
| 20 | +from pandas.io.common import ( |
| 21 | + _NA_VALUES, _is_url, _stringify_path, _validate_header_arg, |
| 22 | + get_filepath_or_buffer) |
20 | 23 | from pandas.io.excel._util import ( |
21 | 24 | _fill_mi_header, _get_default_writer, _maybe_convert_usecols, |
22 | 25 | _pop_header_name, get_writer) |
@@ -329,6 +332,36 @@ def read_excel(io, |
329 | 332 |
|
330 | 333 | class _BaseExcelReader(metaclass=abc.ABCMeta): |
331 | 334 |
|
| 335 | + def __init__(self, filepath_or_buffer): |
| 336 | + # If filepath_or_buffer is a url, load the data into a BytesIO |
| 337 | + if _is_url(filepath_or_buffer): |
| 338 | + filepath_or_buffer = BytesIO(urlopen(filepath_or_buffer).read()) |
| 339 | + elif not isinstance(filepath_or_buffer, |
| 340 | + (ExcelFile, self._workbook_class)): |
| 341 | + filepath_or_buffer, _, _, _ = get_filepath_or_buffer( |
| 342 | + filepath_or_buffer) |
| 343 | + |
| 344 | + if isinstance(filepath_or_buffer, self._workbook_class): |
| 345 | + self.book = filepath_or_buffer |
| 346 | + elif hasattr(filepath_or_buffer, "read"): |
| 347 | + # N.B. xlrd.Book has a read attribute too |
| 348 | + filepath_or_buffer.seek(0) |
| 349 | + self.book = self.load_workbook(filepath_or_buffer) |
| 350 | + elif isinstance(filepath_or_buffer, str): |
| 351 | + self.book = self.load_workbook(filepath_or_buffer) |
| 352 | + else: |
| 353 | + raise ValueError('Must explicitly set engine if not passing in' |
| 354 | + ' buffer or path for io.') |
| 355 | + |
| 356 | + @property |
| 357 | + @abc.abstractmethod |
| 358 | + def _workbook_class(self): |
| 359 | + pass |
| 360 | + |
| 361 | + @abc.abstractmethod |
| 362 | + def load_workbook(self, filepath_or_buffer): |
| 363 | + pass |
| 364 | + |
332 | 365 | @property |
333 | 366 | @abc.abstractmethod |
334 | 367 | def sheet_names(self): |
@@ -701,7 +734,7 @@ def _value_with_fmt(self, val): |
701 | 734 | val = val.total_seconds() / float(86400) |
702 | 735 | fmt = '0' |
703 | 736 | else: |
704 | | - val = compat.to_str(val) |
| 737 | + val = str(val) |
705 | 738 |
|
706 | 739 | return val, fmt |
707 | 740 |
|
|
0 commit comments