_base.py 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. from __future__ import annotations
  2. from collections.abc import (
  3. Hashable,
  4. Iterable,
  5. Mapping,
  6. Sequence,
  7. )
  8. import datetime
  9. from functools import partial
  10. from io import BytesIO
  11. import os
  12. from textwrap import fill
  13. from typing import (
  14. IO,
  15. TYPE_CHECKING,
  16. Any,
  17. Callable,
  18. Generic,
  19. Literal,
  20. TypeVar,
  21. Union,
  22. cast,
  23. overload,
  24. )
  25. import warnings
  26. import zipfile
  27. from pandas._config import config
  28. from pandas._libs import lib
  29. from pandas._libs.parsers import STR_NA_VALUES
  30. from pandas.compat._optional import (
  31. get_version,
  32. import_optional_dependency,
  33. )
  34. from pandas.errors import EmptyDataError
  35. from pandas.util._decorators import (
  36. Appender,
  37. doc,
  38. )
  39. from pandas.util._exceptions import find_stack_level
  40. from pandas.util._validators import check_dtype_backend
  41. from pandas.core.dtypes.common import (
  42. is_bool,
  43. is_float,
  44. is_integer,
  45. is_list_like,
  46. )
  47. from pandas.core.frame import DataFrame
  48. from pandas.core.shared_docs import _shared_docs
  49. from pandas.util.version import Version
  50. from pandas.io.common import (
  51. IOHandles,
  52. get_handle,
  53. stringify_path,
  54. validate_header_arg,
  55. )
  56. from pandas.io.excel._util import (
  57. fill_mi_header,
  58. get_default_engine,
  59. get_writer,
  60. maybe_convert_usecols,
  61. pop_header_name,
  62. )
  63. from pandas.io.parsers import TextParser
  64. from pandas.io.parsers.readers import validate_integer
  65. if TYPE_CHECKING:
  66. from types import TracebackType
  67. from pandas._typing import (
  68. DtypeArg,
  69. DtypeBackend,
  70. ExcelWriterIfSheetExists,
  71. FilePath,
  72. IntStrT,
  73. ReadBuffer,
  74. Self,
  75. SequenceNotStr,
  76. StorageOptions,
  77. WriteExcelBuffer,
  78. )
  79. _read_excel_doc = (
  80. """
  81. Read an Excel file into a ``pandas`` ``DataFrame``.
  82. Supports `xls`, `xlsx`, `xlsm`, `xlsb`, `odf`, `ods` and `odt` file extensions
  83. read from a local filesystem or URL. Supports an option to read
  84. a single sheet or a list of sheets.
  85. Parameters
  86. ----------
  87. io : str, bytes, ExcelFile, xlrd.Book, path object, or file-like object
  88. Any valid string path is acceptable. The string could be a URL. Valid
  89. URL schemes include http, ftp, s3, and file. For file URLs, a host is
  90. expected. A local file could be: ``file://localhost/path/to/table.xlsx``.
  91. If you want to pass in a path object, pandas accepts any ``os.PathLike``.
  92. By file-like object, we refer to objects with a ``read()`` method,
  93. such as a file handle (e.g. via builtin ``open`` function)
  94. or ``StringIO``.
  95. .. deprecated:: 2.1.0
  96. Passing byte strings is deprecated. To read from a
  97. byte string, wrap it in a ``BytesIO`` object.
  98. sheet_name : str, int, list, or None, default 0
  99. Strings are used for sheet names. Integers are used in zero-indexed
  100. sheet positions (chart sheets do not count as a sheet position).
  101. Lists of strings/integers are used to request multiple sheets.
  102. Specify ``None`` to get all worksheets.
  103. Available cases:
  104. * Defaults to ``0``: 1st sheet as a `DataFrame`
  105. * ``1``: 2nd sheet as a `DataFrame`
  106. * ``"Sheet1"``: Load sheet with name "Sheet1"
  107. * ``[0, 1, "Sheet5"]``: Load first, second and sheet named "Sheet5"
  108. as a dict of `DataFrame`
  109. * ``None``: All worksheets.
  110. header : int, list of int, default 0
  111. Row (0-indexed) to use for the column labels of the parsed
  112. DataFrame. If a list of integers is passed those row positions will
  113. be combined into a ``MultiIndex``. Use None if there is no header.
  114. names : array-like, default None
  115. List of column names to use. If file contains no header row,
  116. then you should explicitly pass header=None.
  117. index_col : int, str, list of int, default None
  118. Column (0-indexed) to use as the row labels of the DataFrame.
  119. Pass None if there is no such column. If a list is passed,
  120. those columns will be combined into a ``MultiIndex``. If a
  121. subset of data is selected with ``usecols``, index_col
  122. is based on the subset.
  123. Missing values will be forward filled to allow roundtripping with
  124. ``to_excel`` for ``merged_cells=True``. To avoid forward filling the
  125. missing values use ``set_index`` after reading the data instead of
  126. ``index_col``.
  127. usecols : str, list-like, or callable, default None
  128. * If None, then parse all columns.
  129. * If str, then indicates comma separated list of Excel column letters
  130. and column ranges (e.g. "A:E" or "A,C,E:F"). Ranges are inclusive of
  131. both sides.
  132. * If list of int, then indicates list of column numbers to be parsed
  133. (0-indexed).
  134. * If list of string, then indicates list of column names to be parsed.
  135. * If callable, then evaluate each column name against it and parse the
  136. column if the callable returns ``True``.
  137. Returns a subset of the columns according to behavior above.
  138. dtype : Type name or dict of column -> type, default None
  139. Data type for data or columns. E.g. {{'a': np.float64, 'b': np.int32}}
  140. Use ``object`` to preserve data as stored in Excel and not interpret dtype,
  141. which will necessarily result in ``object`` dtype.
  142. If converters are specified, they will be applied INSTEAD
  143. of dtype conversion.
  144. If you use ``None``, it will infer the dtype of each column based on the data.
  145. engine : {{'openpyxl', 'calamine', 'odf', 'pyxlsb', 'xlrd'}}, default None
  146. If io is not a buffer or path, this must be set to identify io.
  147. Engine compatibility :
  148. - ``openpyxl`` supports newer Excel file formats.
  149. - ``calamine`` supports Excel (.xls, .xlsx, .xlsm, .xlsb)
  150. and OpenDocument (.ods) file formats.
  151. - ``odf`` supports OpenDocument file formats (.odf, .ods, .odt).
  152. - ``pyxlsb`` supports Binary Excel files.
  153. - ``xlrd`` supports old-style Excel files (.xls).
  154. When ``engine=None``, the following logic will be used to determine the engine:
  155. - If ``path_or_buffer`` is an OpenDocument format (.odf, .ods, .odt),
  156. then `odf <https://pypi.org/project/odfpy/>`_ will be used.
  157. - Otherwise if ``path_or_buffer`` is an xls format, ``xlrd`` will be used.
  158. - Otherwise if ``path_or_buffer`` is in xlsb format, ``pyxlsb`` will be used.
  159. - Otherwise ``openpyxl`` will be used.
  160. converters : dict, default None
  161. Dict of functions for converting values in certain columns. Keys can
  162. either be integers or column labels, values are functions that take one
  163. input argument, the Excel cell content, and return the transformed
  164. content.
  165. true_values : list, default None
  166. Values to consider as True.
  167. false_values : list, default None
  168. Values to consider as False.
  169. skiprows : list-like, int, or callable, optional
  170. Line numbers to skip (0-indexed) or number of lines to skip (int) at the
  171. start of the file. If callable, the callable function will be evaluated
  172. against the row indices, returning True if the row should be skipped and
  173. False otherwise. An example of a valid callable argument would be ``lambda
  174. x: x in [0, 2]``.
  175. nrows : int, default None
  176. Number of rows to parse.
  177. na_values : scalar, str, list-like, or dict, default None
  178. Additional strings to recognize as NA/NaN. If dict passed, specific
  179. per-column NA values. By default the following values are interpreted
  180. as NaN: '"""
  181. + fill("', '".join(sorted(STR_NA_VALUES)), 70, subsequent_indent=" ")
  182. + """'.
  183. keep_default_na : bool, default True
  184. Whether or not to include the default NaN values when parsing the data.
  185. Depending on whether ``na_values`` is passed in, the behavior is as follows:
  186. * If ``keep_default_na`` is True, and ``na_values`` are specified,
  187. ``na_values`` is appended to the default NaN values used for parsing.
  188. * If ``keep_default_na`` is True, and ``na_values`` are not specified, only
  189. the default NaN values are used for parsing.
  190. * If ``keep_default_na`` is False, and ``na_values`` are specified, only
  191. the NaN values specified ``na_values`` are used for parsing.
  192. * If ``keep_default_na`` is False, and ``na_values`` are not specified, no
  193. strings will be parsed as NaN.
  194. Note that if `na_filter` is passed in as False, the ``keep_default_na`` and
  195. ``na_values`` parameters will be ignored.
  196. na_filter : bool, default True
  197. Detect missing value markers (empty strings and the value of na_values). In
  198. data without any NAs, passing ``na_filter=False`` can improve the
  199. performance of reading a large file.
  200. verbose : bool, default False
  201. Indicate number of NA values placed in non-numeric columns.
  202. parse_dates : bool, list-like, or dict, default False
  203. The behavior is as follows:
  204. * ``bool``. If True -> try parsing the index.
  205. * ``list`` of int or names. e.g. If [1, 2, 3] -> try parsing columns 1, 2, 3
  206. each as a separate date column.
  207. * ``list`` of lists. e.g. If [[1, 3]] -> combine columns 1 and 3 and parse as
  208. a single date column.
  209. * ``dict``, e.g. {{'foo' : [1, 3]}} -> parse columns 1, 3 as date and call
  210. result 'foo'
  211. If a column or index contains an unparsable date, the entire column or
  212. index will be returned unaltered as an object data type. If you don`t want to
  213. parse some cells as date just change their type in Excel to "Text".
  214. For non-standard datetime parsing, use ``pd.to_datetime`` after ``pd.read_excel``.
  215. Note: A fast-path exists for iso8601-formatted dates.
  216. date_parser : function, optional
  217. Function to use for converting a sequence of string columns to an array of
  218. datetime instances. The default uses ``dateutil.parser.parser`` to do the
  219. conversion. Pandas will try to call `date_parser` in three different ways,
  220. advancing to the next if an exception occurs: 1) Pass one or more arrays
  221. (as defined by `parse_dates`) as arguments; 2) concatenate (row-wise) the
  222. string values from the columns defined by `parse_dates` into a single array
  223. and pass that; and 3) call `date_parser` once for each row using one or
  224. more strings (corresponding to the columns defined by `parse_dates`) as
  225. arguments.
  226. .. deprecated:: 2.0.0
  227. Use ``date_format`` instead, or read in as ``object`` and then apply
  228. :func:`to_datetime` as-needed.
  229. date_format : str or dict of column -> format, default ``None``
  230. If used in conjunction with ``parse_dates``, will parse dates according to this
  231. format. For anything more complex,
  232. please read in as ``object`` and then apply :func:`to_datetime` as-needed.
  233. .. versionadded:: 2.0.0
  234. thousands : str, default None
  235. Thousands separator for parsing string columns to numeric. Note that
  236. this parameter is only necessary for columns stored as TEXT in Excel,
  237. any numeric columns will automatically be parsed, regardless of display
  238. format.
  239. decimal : str, default '.'
  240. Character to recognize as decimal point for parsing string columns to numeric.
  241. Note that this parameter is only necessary for columns stored as TEXT in Excel,
  242. any numeric columns will automatically be parsed, regardless of display
  243. format.(e.g. use ',' for European data).
  244. .. versionadded:: 1.4.0
  245. comment : str, default None
  246. Comments out remainder of line. Pass a character or characters to this
  247. argument to indicate comments in the input file. Any data between the
  248. comment string and the end of the current line is ignored.
  249. skipfooter : int, default 0
  250. Rows at the end to skip (0-indexed).
  251. {storage_options}
  252. dtype_backend : {{'numpy_nullable', 'pyarrow'}}, default 'numpy_nullable'
  253. Back-end data type applied to the resultant :class:`DataFrame`
  254. (still experimental). Behaviour is as follows:
  255. * ``"numpy_nullable"``: returns nullable-dtype-backed :class:`DataFrame`
  256. (default).
  257. * ``"pyarrow"``: returns pyarrow-backed nullable :class:`ArrowDtype`
  258. DataFrame.
  259. .. versionadded:: 2.0
  260. engine_kwargs : dict, optional
  261. Arbitrary keyword arguments passed to excel engine.
  262. Returns
  263. -------
  264. DataFrame or dict of DataFrames
  265. DataFrame from the passed in Excel file. See notes in sheet_name
  266. argument for more information on when a dict of DataFrames is returned.
  267. See Also
  268. --------
  269. DataFrame.to_excel : Write DataFrame to an Excel file.
  270. DataFrame.to_csv : Write DataFrame to a comma-separated values (csv) file.
  271. read_csv : Read a comma-separated values (csv) file into DataFrame.
  272. read_fwf : Read a table of fixed-width formatted lines into DataFrame.
  273. Notes
  274. -----
  275. For specific information on the methods used for each Excel engine, refer to the pandas
  276. :ref:`user guide <io.excel_reader>`
  277. Examples
  278. --------
  279. The file can be read using the file name as string or an open file object:
  280. >>> pd.read_excel('tmp.xlsx', index_col=0) # doctest: +SKIP
  281. Name Value
  282. 0 string1 1
  283. 1 string2 2
  284. 2 #Comment 3
  285. >>> pd.read_excel(open('tmp.xlsx', 'rb'),
  286. ... sheet_name='Sheet3') # doctest: +SKIP
  287. Unnamed: 0 Name Value
  288. 0 0 string1 1
  289. 1 1 string2 2
  290. 2 2 #Comment 3
  291. Index and header can be specified via the `index_col` and `header` arguments
  292. >>> pd.read_excel('tmp.xlsx', index_col=None, header=None) # doctest: +SKIP
  293. 0 1 2
  294. 0 NaN Name Value
  295. 1 0.0 string1 1
  296. 2 1.0 string2 2
  297. 3 2.0 #Comment 3
  298. Column types are inferred but can be explicitly specified
  299. >>> pd.read_excel('tmp.xlsx', index_col=0,
  300. ... dtype={{'Name': str, 'Value': float}}) # doctest: +SKIP
  301. Name Value
  302. 0 string1 1.0
  303. 1 string2 2.0
  304. 2 #Comment 3.0
  305. True, False, and NA values, and thousands separators have defaults,
  306. but can be explicitly specified, too. Supply the values you would like
  307. as strings or lists of strings!
  308. >>> pd.read_excel('tmp.xlsx', index_col=0,
  309. ... na_values=['string1', 'string2']) # doctest: +SKIP
  310. Name Value
  311. 0 NaN 1
  312. 1 NaN 2
  313. 2 #Comment 3
  314. Comment lines in the excel input file can be skipped using the
  315. ``comment`` kwarg.
  316. >>> pd.read_excel('tmp.xlsx', index_col=0, comment='#') # doctest: +SKIP
  317. Name Value
  318. 0 string1 1.0
  319. 1 string2 2.0
  320. 2 None NaN
  321. """
  322. )
  323. @overload
  324. def read_excel(
  325. io,
  326. # sheet name is str or int -> DataFrame
  327. sheet_name: str | int = ...,
  328. *,
  329. header: int | Sequence[int] | None = ...,
  330. names: SequenceNotStr[Hashable] | range | None = ...,
  331. index_col: int | str | Sequence[int] | None = ...,
  332. usecols: int
  333. | str
  334. | Sequence[int]
  335. | Sequence[str]
  336. | Callable[[str], bool]
  337. | None = ...,
  338. dtype: DtypeArg | None = ...,
  339. engine: Literal["xlrd", "openpyxl", "odf", "pyxlsb", "calamine"] | None = ...,
  340. converters: dict[str, Callable] | dict[int, Callable] | None = ...,
  341. true_values: Iterable[Hashable] | None = ...,
  342. false_values: Iterable[Hashable] | None = ...,
  343. skiprows: Sequence[int] | int | Callable[[int], object] | None = ...,
  344. nrows: int | None = ...,
  345. na_values=...,
  346. keep_default_na: bool = ...,
  347. na_filter: bool = ...,
  348. verbose: bool = ...,
  349. parse_dates: list | dict | bool = ...,
  350. date_parser: Callable | lib.NoDefault = ...,
  351. date_format: dict[Hashable, str] | str | None = ...,
  352. thousands: str | None = ...,
  353. decimal: str = ...,
  354. comment: str | None = ...,
  355. skipfooter: int = ...,
  356. storage_options: StorageOptions = ...,
  357. dtype_backend: DtypeBackend | lib.NoDefault = ...,
  358. ) -> DataFrame:
  359. ...
  360. @overload
  361. def read_excel(
  362. io,
  363. # sheet name is list or None -> dict[IntStrT, DataFrame]
  364. sheet_name: list[IntStrT] | None,
  365. *,
  366. header: int | Sequence[int] | None = ...,
  367. names: SequenceNotStr[Hashable] | range | None = ...,
  368. index_col: int | str | Sequence[int] | None = ...,
  369. usecols: int
  370. | str
  371. | Sequence[int]
  372. | Sequence[str]
  373. | Callable[[str], bool]
  374. | None = ...,
  375. dtype: DtypeArg | None = ...,
  376. engine: Literal["xlrd", "openpyxl", "odf", "pyxlsb", "calamine"] | None = ...,
  377. converters: dict[str, Callable] | dict[int, Callable] | None = ...,
  378. true_values: Iterable[Hashable] | None = ...,
  379. false_values: Iterable[Hashable] | None = ...,
  380. skiprows: Sequence[int] | int | Callable[[int], object] | None = ...,
  381. nrows: int | None = ...,
  382. na_values=...,
  383. keep_default_na: bool = ...,
  384. na_filter: bool = ...,
  385. verbose: bool = ...,
  386. parse_dates: list | dict | bool = ...,
  387. date_parser: Callable | lib.NoDefault = ...,
  388. date_format: dict[Hashable, str] | str | None = ...,
  389. thousands: str | None = ...,
  390. decimal: str = ...,
  391. comment: str | None = ...,
  392. skipfooter: int = ...,
  393. storage_options: StorageOptions = ...,
  394. dtype_backend: DtypeBackend | lib.NoDefault = ...,
  395. ) -> dict[IntStrT, DataFrame]:
  396. ...
  397. @doc(storage_options=_shared_docs["storage_options"])
  398. @Appender(_read_excel_doc)
  399. def read_excel(
  400. io,
  401. sheet_name: str | int | list[IntStrT] | None = 0,
  402. *,
  403. header: int | Sequence[int] | None = 0,
  404. names: SequenceNotStr[Hashable] | range | None = None,
  405. index_col: int | str | Sequence[int] | None = None,
  406. usecols: int
  407. | str
  408. | Sequence[int]
  409. | Sequence[str]
  410. | Callable[[str], bool]
  411. | None = None,
  412. dtype: DtypeArg | None = None,
  413. engine: Literal["xlrd", "openpyxl", "odf", "pyxlsb", "calamine"] | None = None,
  414. converters: dict[str, Callable] | dict[int, Callable] | None = None,
  415. true_values: Iterable[Hashable] | None = None,
  416. false_values: Iterable[Hashable] | None = None,
  417. skiprows: Sequence[int] | int | Callable[[int], object] | None = None,
  418. nrows: int | None = None,
  419. na_values=None,
  420. keep_default_na: bool = True,
  421. na_filter: bool = True,
  422. verbose: bool = False,
  423. parse_dates: list | dict | bool = False,
  424. date_parser: Callable | lib.NoDefault = lib.no_default,
  425. date_format: dict[Hashable, str] | str | None = None,
  426. thousands: str | None = None,
  427. decimal: str = ".",
  428. comment: str | None = None,
  429. skipfooter: int = 0,
  430. storage_options: StorageOptions | None = None,
  431. dtype_backend: DtypeBackend | lib.NoDefault = lib.no_default,
  432. engine_kwargs: dict | None = None,
  433. ) -> DataFrame | dict[IntStrT, DataFrame]:
  434. check_dtype_backend(dtype_backend)
  435. should_close = False
  436. if engine_kwargs is None:
  437. engine_kwargs = {}
  438. if not isinstance(io, ExcelFile):
  439. should_close = True
  440. io = ExcelFile(
  441. io,
  442. storage_options=storage_options,
  443. engine=engine,
  444. engine_kwargs=engine_kwargs,
  445. )
  446. elif engine and engine != io.engine:
  447. raise ValueError(
  448. "Engine should not be specified when passing "
  449. "an ExcelFile - ExcelFile already has the engine set"
  450. )
  451. try:
  452. data = io.parse(
  453. sheet_name=sheet_name,
  454. header=header,
  455. names=names,
  456. index_col=index_col,
  457. usecols=usecols,
  458. dtype=dtype,
  459. converters=converters,
  460. true_values=true_values,
  461. false_values=false_values,
  462. skiprows=skiprows,
  463. nrows=nrows,
  464. na_values=na_values,
  465. keep_default_na=keep_default_na,
  466. na_filter=na_filter,
  467. verbose=verbose,
  468. parse_dates=parse_dates,
  469. date_parser=date_parser,
  470. date_format=date_format,
  471. thousands=thousands,
  472. decimal=decimal,
  473. comment=comment,
  474. skipfooter=skipfooter,
  475. dtype_backend=dtype_backend,
  476. )
  477. finally:
  478. # make sure to close opened file handles
  479. if should_close:
  480. io.close()
  481. return data
  482. _WorkbookT = TypeVar("_WorkbookT")
  483. class BaseExcelReader(Generic[_WorkbookT]):
  484. book: _WorkbookT
  485. def __init__(
  486. self,
  487. filepath_or_buffer,
  488. storage_options: StorageOptions | None = None,
  489. engine_kwargs: dict | None = None,
  490. ) -> None:
  491. if engine_kwargs is None:
  492. engine_kwargs = {}
  493. # First argument can also be bytes, so create a buffer
  494. if isinstance(filepath_or_buffer, bytes):
  495. filepath_or_buffer = BytesIO(filepath_or_buffer)
  496. self.handles = IOHandles(
  497. handle=filepath_or_buffer, compression={"method": None}
  498. )
  499. if not isinstance(filepath_or_buffer, (ExcelFile, self._workbook_class)):
  500. self.handles = get_handle(
  501. filepath_or_buffer, "rb", storage_options=storage_options, is_text=False
  502. )
  503. if isinstance(self.handles.handle, self._workbook_class):
  504. self.book = self.handles.handle
  505. elif hasattr(self.handles.handle, "read"):
  506. # N.B. xlrd.Book has a read attribute too
  507. self.handles.handle.seek(0)
  508. try:
  509. self.book = self.load_workbook(self.handles.handle, engine_kwargs)
  510. except Exception:
  511. self.close()
  512. raise
  513. else:
  514. raise ValueError(
  515. "Must explicitly set engine if not passing in buffer or path for io."
  516. )
  517. @property
  518. def _workbook_class(self) -> type[_WorkbookT]:
  519. raise NotImplementedError
  520. def load_workbook(self, filepath_or_buffer, engine_kwargs) -> _WorkbookT:
  521. raise NotImplementedError
  522. def close(self) -> None:
  523. if hasattr(self, "book"):
  524. if hasattr(self.book, "close"):
  525. # pyxlsb: opens a TemporaryFile
  526. # openpyxl: https://stackoverflow.com/questions/31416842/
  527. # openpyxl-does-not-close-excel-workbook-in-read-only-mode
  528. self.book.close()
  529. elif hasattr(self.book, "release_resources"):
  530. # xlrd
  531. # https://github.com/python-excel/xlrd/blob/2.0.1/xlrd/book.py#L548
  532. self.book.release_resources()
  533. self.handles.close()
  534. @property
  535. def sheet_names(self) -> list[str]:
  536. raise NotImplementedError
  537. def get_sheet_by_name(self, name: str):
  538. raise NotImplementedError
  539. def get_sheet_by_index(self, index: int):
  540. raise NotImplementedError
  541. def get_sheet_data(self, sheet, rows: int | None = None):
  542. raise NotImplementedError
  543. def raise_if_bad_sheet_by_index(self, index: int) -> None:
  544. n_sheets = len(self.sheet_names)
  545. if index >= n_sheets:
  546. raise ValueError(
  547. f"Worksheet index {index} is invalid, {n_sheets} worksheets found"
  548. )
  549. def raise_if_bad_sheet_by_name(self, name: str) -> None:
  550. if name not in self.sheet_names:
  551. raise ValueError(f"Worksheet named '{name}' not found")
  552. def _check_skiprows_func(
  553. self,
  554. skiprows: Callable,
  555. rows_to_use: int,
  556. ) -> int:
  557. """
  558. Determine how many file rows are required to obtain `nrows` data
  559. rows when `skiprows` is a function.
  560. Parameters
  561. ----------
  562. skiprows : function
  563. The function passed to read_excel by the user.
  564. rows_to_use : int
  565. The number of rows that will be needed for the header and
  566. the data.
  567. Returns
  568. -------
  569. int
  570. """
  571. i = 0
  572. rows_used_so_far = 0
  573. while rows_used_so_far < rows_to_use:
  574. if not skiprows(i):
  575. rows_used_so_far += 1
  576. i += 1
  577. return i
  578. def _calc_rows(
  579. self,
  580. header: int | Sequence[int] | None,
  581. index_col: int | Sequence[int] | None,
  582. skiprows: Sequence[int] | int | Callable[[int], object] | None,
  583. nrows: int | None,
  584. ) -> int | None:
  585. """
  586. If nrows specified, find the number of rows needed from the
  587. file, otherwise return None.
  588. Parameters
  589. ----------
  590. header : int, list of int, or None
  591. See read_excel docstring.
  592. index_col : int, str, list of int, or None
  593. See read_excel docstring.
  594. skiprows : list-like, int, callable, or None
  595. See read_excel docstring.
  596. nrows : int or None
  597. See read_excel docstring.
  598. Returns
  599. -------
  600. int or None
  601. """
  602. if nrows is None:
  603. return None
  604. if header is None:
  605. header_rows = 1
  606. elif is_integer(header):
  607. header = cast(int, header)
  608. header_rows = 1 + header
  609. else:
  610. header = cast(Sequence, header)
  611. header_rows = 1 + header[-1]
  612. # If there is a MultiIndex header and an index then there is also
  613. # a row containing just the index name(s)
  614. if is_list_like(header) and index_col is not None:
  615. header = cast(Sequence, header)
  616. if len(header) > 1:
  617. header_rows += 1
  618. if skiprows is None:
  619. return header_rows + nrows
  620. if is_integer(skiprows):
  621. skiprows = cast(int, skiprows)
  622. return header_rows + nrows + skiprows
  623. if is_list_like(skiprows):
  624. def f(skiprows: Sequence, x: int) -> bool:
  625. return x in skiprows
  626. skiprows = cast(Sequence, skiprows)
  627. return self._check_skiprows_func(partial(f, skiprows), header_rows + nrows)
  628. if callable(skiprows):
  629. return self._check_skiprows_func(
  630. skiprows,
  631. header_rows + nrows,
  632. )
  633. # else unexpected skiprows type: read_excel will not optimize
  634. # the number of rows read from file
  635. return None
  636. def parse(
  637. self,
  638. sheet_name: str | int | list[int] | list[str] | None = 0,
  639. header: int | Sequence[int] | None = 0,
  640. names: SequenceNotStr[Hashable] | range | None = None,
  641. index_col: int | Sequence[int] | None = None,
  642. usecols=None,
  643. dtype: DtypeArg | None = None,
  644. true_values: Iterable[Hashable] | None = None,
  645. false_values: Iterable[Hashable] | None = None,
  646. skiprows: Sequence[int] | int | Callable[[int], object] | None = None,
  647. nrows: int | None = None,
  648. na_values=None,
  649. verbose: bool = False,
  650. parse_dates: list | dict | bool = False,
  651. date_parser: Callable | lib.NoDefault = lib.no_default,
  652. date_format: dict[Hashable, str] | str | None = None,
  653. thousands: str | None = None,
  654. decimal: str = ".",
  655. comment: str | None = None,
  656. skipfooter: int = 0,
  657. dtype_backend: DtypeBackend | lib.NoDefault = lib.no_default,
  658. **kwds,
  659. ):
  660. validate_header_arg(header)
  661. validate_integer("nrows", nrows)
  662. ret_dict = False
  663. # Keep sheetname to maintain backwards compatibility.
  664. sheets: list[int] | list[str]
  665. if isinstance(sheet_name, list):
  666. sheets = sheet_name
  667. ret_dict = True
  668. elif sheet_name is None:
  669. sheets = self.sheet_names
  670. ret_dict = True
  671. elif isinstance(sheet_name, str):
  672. sheets = [sheet_name]
  673. else:
  674. sheets = [sheet_name]
  675. # handle same-type duplicates.
  676. sheets = cast(Union[list[int], list[str]], list(dict.fromkeys(sheets).keys()))
  677. output = {}
  678. last_sheetname = None
  679. for asheetname in sheets:
  680. last_sheetname = asheetname
  681. if verbose:
  682. print(f"Reading sheet {asheetname}")
  683. if isinstance(asheetname, str):
  684. sheet = self.get_sheet_by_name(asheetname)
  685. else: # assume an integer if not a string
  686. sheet = self.get_sheet_by_index(asheetname)
  687. file_rows_needed = self._calc_rows(header, index_col, skiprows, nrows)
  688. data = self.get_sheet_data(sheet, file_rows_needed)
  689. if hasattr(sheet, "close"):
  690. # pyxlsb opens two TemporaryFiles
  691. sheet.close()
  692. usecols = maybe_convert_usecols(usecols)
  693. if not data:
  694. output[asheetname] = DataFrame()
  695. continue
  696. is_list_header = False
  697. is_len_one_list_header = False
  698. if is_list_like(header):
  699. assert isinstance(header, Sequence)
  700. is_list_header = True
  701. if len(header) == 1:
  702. is_len_one_list_header = True
  703. if is_len_one_list_header:
  704. header = cast(Sequence[int], header)[0]
  705. # forward fill and pull out names for MultiIndex column
  706. header_names = None
  707. if header is not None and is_list_like(header):
  708. assert isinstance(header, Sequence)
  709. header_names = []
  710. control_row = [True] * len(data[0])
  711. for row in header:
  712. if is_integer(skiprows):
  713. assert isinstance(skiprows, int)
  714. row += skiprows
  715. if row > len(data) - 1:
  716. raise ValueError(
  717. f"header index {row} exceeds maximum index "
  718. f"{len(data) - 1} of data.",
  719. )
  720. data[row], control_row = fill_mi_header(data[row], control_row)
  721. if index_col is not None:
  722. header_name, _ = pop_header_name(data[row], index_col)
  723. header_names.append(header_name)
  724. # If there is a MultiIndex header and an index then there is also
  725. # a row containing just the index name(s)
  726. has_index_names = False
  727. if is_list_header and not is_len_one_list_header and index_col is not None:
  728. index_col_list: Sequence[int]
  729. if isinstance(index_col, int):
  730. index_col_list = [index_col]
  731. else:
  732. assert isinstance(index_col, Sequence)
  733. index_col_list = index_col
  734. # We have to handle mi without names. If any of the entries in the data
  735. # columns are not empty, this is a regular row
  736. assert isinstance(header, Sequence)
  737. if len(header) < len(data):
  738. potential_index_names = data[len(header)]
  739. potential_data = [
  740. x
  741. for i, x in enumerate(potential_index_names)
  742. if not control_row[i] and i not in index_col_list
  743. ]
  744. has_index_names = all(x == "" or x is None for x in potential_data)
  745. if is_list_like(index_col):
  746. # Forward fill values for MultiIndex index.
  747. if header is None:
  748. offset = 0
  749. elif isinstance(header, int):
  750. offset = 1 + header
  751. else:
  752. offset = 1 + max(header)
  753. # GH34673: if MultiIndex names present and not defined in the header,
  754. # offset needs to be incremented so that forward filling starts
  755. # from the first MI value instead of the name
  756. if has_index_names:
  757. offset += 1
  758. # Check if we have an empty dataset
  759. # before trying to collect data.
  760. if offset < len(data):
  761. assert isinstance(index_col, Sequence)
  762. for col in index_col:
  763. last = data[offset][col]
  764. for row in range(offset + 1, len(data)):
  765. if data[row][col] == "" or data[row][col] is None:
  766. data[row][col] = last
  767. else:
  768. last = data[row][col]
  769. # GH 12292 : error when read one empty column from excel file
  770. try:
  771. parser = TextParser(
  772. data,
  773. names=names,
  774. header=header,
  775. index_col=index_col,
  776. has_index_names=has_index_names,
  777. dtype=dtype,
  778. true_values=true_values,
  779. false_values=false_values,
  780. skiprows=skiprows,
  781. nrows=nrows,
  782. na_values=na_values,
  783. skip_blank_lines=False, # GH 39808
  784. parse_dates=parse_dates,
  785. date_parser=date_parser,
  786. date_format=date_format,
  787. thousands=thousands,
  788. decimal=decimal,
  789. comment=comment,
  790. skipfooter=skipfooter,
  791. usecols=usecols,
  792. dtype_backend=dtype_backend,
  793. **kwds,
  794. )
  795. output[asheetname] = parser.read(nrows=nrows)
  796. if header_names:
  797. output[asheetname].columns = output[asheetname].columns.set_names(
  798. header_names
  799. )
  800. except EmptyDataError:
  801. # No Data, return an empty DataFrame
  802. output[asheetname] = DataFrame()
  803. except Exception as err:
  804. err.args = (f"{err.args[0]} (sheet: {asheetname})", *err.args[1:])
  805. raise err
  806. if last_sheetname is None:
  807. raise ValueError("Sheet name is an empty list")
  808. if ret_dict:
  809. return output
  810. else:
  811. return output[last_sheetname]
  812. @doc(storage_options=_shared_docs["storage_options"])
  813. class ExcelWriter(Generic[_WorkbookT]):
  814. """
  815. Class for writing DataFrame objects into excel sheets.
  816. Default is to use:
  817. * `xlsxwriter <https://pypi.org/project/XlsxWriter/>`__ for xlsx files if xlsxwriter
  818. is installed otherwise `openpyxl <https://pypi.org/project/openpyxl/>`__
  819. * `odswriter <https://pypi.org/project/odswriter/>`__ for ods files
  820. See ``DataFrame.to_excel`` for typical usage.
  821. The writer should be used as a context manager. Otherwise, call `close()` to save
  822. and close any opened file handles.
  823. Parameters
  824. ----------
  825. path : str or typing.BinaryIO
  826. Path to xls or xlsx or ods file.
  827. engine : str (optional)
  828. Engine to use for writing. If None, defaults to
  829. ``io.excel.<extension>.writer``. NOTE: can only be passed as a keyword
  830. argument.
  831. date_format : str, default None
  832. Format string for dates written into Excel files (e.g. 'YYYY-MM-DD').
  833. datetime_format : str, default None
  834. Format string for datetime objects written into Excel files.
  835. (e.g. 'YYYY-MM-DD HH:MM:SS').
  836. mode : {{'w', 'a'}}, default 'w'
  837. File mode to use (write or append). Append does not work with fsspec URLs.
  838. {storage_options}
  839. if_sheet_exists : {{'error', 'new', 'replace', 'overlay'}}, default 'error'
  840. How to behave when trying to write to a sheet that already
  841. exists (append mode only).
  842. * error: raise a ValueError.
  843. * new: Create a new sheet, with a name determined by the engine.
  844. * replace: Delete the contents of the sheet before writing to it.
  845. * overlay: Write contents to the existing sheet without first removing,
  846. but possibly over top of, the existing contents.
  847. .. versionadded:: 1.3.0
  848. .. versionchanged:: 1.4.0
  849. Added ``overlay`` option
  850. engine_kwargs : dict, optional
  851. Keyword arguments to be passed into the engine. These will be passed to
  852. the following functions of the respective engines:
  853. * xlsxwriter: ``xlsxwriter.Workbook(file, **engine_kwargs)``
  854. * openpyxl (write mode): ``openpyxl.Workbook(**engine_kwargs)``
  855. * openpyxl (append mode): ``openpyxl.load_workbook(file, **engine_kwargs)``
  856. * odswriter: ``odf.opendocument.OpenDocumentSpreadsheet(**engine_kwargs)``
  857. .. versionadded:: 1.3.0
  858. Notes
  859. -----
  860. For compatibility with CSV writers, ExcelWriter serializes lists
  861. and dicts to strings before writing.
  862. Examples
  863. --------
  864. Default usage:
  865. >>> df = pd.DataFrame([["ABC", "XYZ"]], columns=["Foo", "Bar"]) # doctest: +SKIP
  866. >>> with pd.ExcelWriter("path_to_file.xlsx") as writer:
  867. ... df.to_excel(writer) # doctest: +SKIP
  868. To write to separate sheets in a single file:
  869. >>> df1 = pd.DataFrame([["AAA", "BBB"]], columns=["Spam", "Egg"]) # doctest: +SKIP
  870. >>> df2 = pd.DataFrame([["ABC", "XYZ"]], columns=["Foo", "Bar"]) # doctest: +SKIP
  871. >>> with pd.ExcelWriter("path_to_file.xlsx") as writer:
  872. ... df1.to_excel(writer, sheet_name="Sheet1") # doctest: +SKIP
  873. ... df2.to_excel(writer, sheet_name="Sheet2") # doctest: +SKIP
  874. You can set the date format or datetime format:
  875. >>> from datetime import date, datetime # doctest: +SKIP
  876. >>> df = pd.DataFrame(
  877. ... [
  878. ... [date(2014, 1, 31), date(1999, 9, 24)],
  879. ... [datetime(1998, 5, 26, 23, 33, 4), datetime(2014, 2, 28, 13, 5, 13)],
  880. ... ],
  881. ... index=["Date", "Datetime"],
  882. ... columns=["X", "Y"],
  883. ... ) # doctest: +SKIP
  884. >>> with pd.ExcelWriter(
  885. ... "path_to_file.xlsx",
  886. ... date_format="YYYY-MM-DD",
  887. ... datetime_format="YYYY-MM-DD HH:MM:SS"
  888. ... ) as writer:
  889. ... df.to_excel(writer) # doctest: +SKIP
  890. You can also append to an existing Excel file:
  891. >>> with pd.ExcelWriter("path_to_file.xlsx", mode="a", engine="openpyxl") as writer:
  892. ... df.to_excel(writer, sheet_name="Sheet3") # doctest: +SKIP
  893. Here, the `if_sheet_exists` parameter can be set to replace a sheet if it
  894. already exists:
  895. >>> with ExcelWriter(
  896. ... "path_to_file.xlsx",
  897. ... mode="a",
  898. ... engine="openpyxl",
  899. ... if_sheet_exists="replace",
  900. ... ) as writer:
  901. ... df.to_excel(writer, sheet_name="Sheet1") # doctest: +SKIP
  902. You can also write multiple DataFrames to a single sheet. Note that the
  903. ``if_sheet_exists`` parameter needs to be set to ``overlay``:
  904. >>> with ExcelWriter("path_to_file.xlsx",
  905. ... mode="a",
  906. ... engine="openpyxl",
  907. ... if_sheet_exists="overlay",
  908. ... ) as writer:
  909. ... df1.to_excel(writer, sheet_name="Sheet1")
  910. ... df2.to_excel(writer, sheet_name="Sheet1", startcol=3) # doctest: +SKIP
  911. You can store Excel file in RAM:
  912. >>> import io
  913. >>> df = pd.DataFrame([["ABC", "XYZ"]], columns=["Foo", "Bar"])
  914. >>> buffer = io.BytesIO()
  915. >>> with pd.ExcelWriter(buffer) as writer:
  916. ... df.to_excel(writer)
  917. You can pack Excel file into zip archive:
  918. >>> import zipfile # doctest: +SKIP
  919. >>> df = pd.DataFrame([["ABC", "XYZ"]], columns=["Foo", "Bar"]) # doctest: +SKIP
  920. >>> with zipfile.ZipFile("path_to_file.zip", "w") as zf:
  921. ... with zf.open("filename.xlsx", "w") as buffer:
  922. ... with pd.ExcelWriter(buffer) as writer:
  923. ... df.to_excel(writer) # doctest: +SKIP
  924. You can specify additional arguments to the underlying engine:
  925. >>> with pd.ExcelWriter(
  926. ... "path_to_file.xlsx",
  927. ... engine="xlsxwriter",
  928. ... engine_kwargs={{"options": {{"nan_inf_to_errors": True}}}}
  929. ... ) as writer:
  930. ... df.to_excel(writer) # doctest: +SKIP
  931. In append mode, ``engine_kwargs`` are passed through to
  932. openpyxl's ``load_workbook``:
  933. >>> with pd.ExcelWriter(
  934. ... "path_to_file.xlsx",
  935. ... engine="openpyxl",
  936. ... mode="a",
  937. ... engine_kwargs={{"keep_vba": True}}
  938. ... ) as writer:
  939. ... df.to_excel(writer, sheet_name="Sheet2") # doctest: +SKIP
  940. """
  941. # Defining an ExcelWriter implementation (see abstract methods for more...)
  942. # - Mandatory
  943. # - ``write_cells(self, cells, sheet_name=None, startrow=0, startcol=0)``
  944. # --> called to write additional DataFrames to disk
  945. # - ``_supported_extensions`` (tuple of supported extensions), used to
  946. # check that engine supports the given extension.
  947. # - ``_engine`` - string that gives the engine name. Necessary to
  948. # instantiate class directly and bypass ``ExcelWriterMeta`` engine
  949. # lookup.
  950. # - ``save(self)`` --> called to save file to disk
  951. # - Mostly mandatory (i.e. should at least exist)
  952. # - book, cur_sheet, path
  953. # - Optional:
  954. # - ``__init__(self, path, engine=None, **kwargs)`` --> always called
  955. # with path as first argument.
  956. # You also need to register the class with ``register_writer()``.
  957. # Technically, ExcelWriter implementations don't need to subclass
  958. # ExcelWriter.
  959. _engine: str
  960. _supported_extensions: tuple[str, ...]
  961. def __new__(
  962. cls,
  963. path: FilePath | WriteExcelBuffer | ExcelWriter,
  964. engine: str | None = None,
  965. date_format: str | None = None,
  966. datetime_format: str | None = None,
  967. mode: str = "w",
  968. storage_options: StorageOptions | None = None,
  969. if_sheet_exists: ExcelWriterIfSheetExists | None = None,
  970. engine_kwargs: dict | None = None,
  971. ) -> Self:
  972. # only switch class if generic(ExcelWriter)
  973. if cls is ExcelWriter:
  974. if engine is None or (isinstance(engine, str) and engine == "auto"):
  975. if isinstance(path, str):
  976. ext = os.path.splitext(path)[-1][1:]
  977. else:
  978. ext = "xlsx"
  979. try:
  980. engine = config.get_option(f"io.excel.{ext}.writer", silent=True)
  981. if engine == "auto":
  982. engine = get_default_engine(ext, mode="writer")
  983. except KeyError as err:
  984. raise ValueError(f"No engine for filetype: '{ext}'") from err
  985. # for mypy
  986. assert engine is not None
  987. # error: Incompatible types in assignment (expression has type
  988. # "type[ExcelWriter[Any]]", variable has type "type[Self]")
  989. cls = get_writer(engine) # type: ignore[assignment]
  990. return object.__new__(cls)
  991. # declare external properties you can count on
  992. _path = None
  993. @property
  994. def supported_extensions(self) -> tuple[str, ...]:
  995. """Extensions that writer engine supports."""
  996. return self._supported_extensions
  997. @property
  998. def engine(self) -> str:
  999. """Name of engine."""
  1000. return self._engine
  1001. @property
  1002. def sheets(self) -> dict[str, Any]:
  1003. """Mapping of sheet names to sheet objects."""
  1004. raise NotImplementedError
  1005. @property
  1006. def book(self) -> _WorkbookT:
  1007. """
  1008. Book instance. Class type will depend on the engine used.
  1009. This attribute can be used to access engine-specific features.
  1010. """
  1011. raise NotImplementedError
  1012. def _write_cells(
  1013. self,
  1014. cells,
  1015. sheet_name: str | None = None,
  1016. startrow: int = 0,
  1017. startcol: int = 0,
  1018. freeze_panes: tuple[int, int] | None = None,
  1019. ) -> None:
  1020. """
  1021. Write given formatted cells into Excel an excel sheet
  1022. Parameters
  1023. ----------
  1024. cells : generator
  1025. cell of formatted data to save to Excel sheet
  1026. sheet_name : str, default None
  1027. Name of Excel sheet, if None, then use self.cur_sheet
  1028. startrow : upper left cell row to dump data frame
  1029. startcol : upper left cell column to dump data frame
  1030. freeze_panes: int tuple of length 2
  1031. contains the bottom-most row and right-most column to freeze
  1032. """
  1033. raise NotImplementedError
  1034. def _save(self) -> None:
  1035. """
  1036. Save workbook to disk.
  1037. """
  1038. raise NotImplementedError
  1039. def __init__(
  1040. self,
  1041. path: FilePath | WriteExcelBuffer | ExcelWriter,
  1042. engine: str | None = None,
  1043. date_format: str | None = None,
  1044. datetime_format: str | None = None,
  1045. mode: str = "w",
  1046. storage_options: StorageOptions | None = None,
  1047. if_sheet_exists: ExcelWriterIfSheetExists | None = None,
  1048. engine_kwargs: dict[str, Any] | None = None,
  1049. ) -> None:
  1050. # validate that this engine can handle the extension
  1051. if isinstance(path, str):
  1052. ext = os.path.splitext(path)[-1]
  1053. self.check_extension(ext)
  1054. # use mode to open the file
  1055. if "b" not in mode:
  1056. mode += "b"
  1057. # use "a" for the user to append data to excel but internally use "r+" to let
  1058. # the excel backend first read the existing file and then write any data to it
  1059. mode = mode.replace("a", "r+")
  1060. if if_sheet_exists not in (None, "error", "new", "replace", "overlay"):
  1061. raise ValueError(
  1062. f"'{if_sheet_exists}' is not valid for if_sheet_exists. "
  1063. "Valid options are 'error', 'new', 'replace' and 'overlay'."
  1064. )
  1065. if if_sheet_exists and "r+" not in mode:
  1066. raise ValueError("if_sheet_exists is only valid in append mode (mode='a')")
  1067. if if_sheet_exists is None:
  1068. if_sheet_exists = "error"
  1069. self._if_sheet_exists = if_sheet_exists
  1070. # cast ExcelWriter to avoid adding 'if self._handles is not None'
  1071. self._handles = IOHandles(
  1072. cast(IO[bytes], path), compression={"compression": None}
  1073. )
  1074. if not isinstance(path, ExcelWriter):
  1075. self._handles = get_handle(
  1076. path, mode, storage_options=storage_options, is_text=False
  1077. )
  1078. self._cur_sheet = None
  1079. if date_format is None:
  1080. self._date_format = "YYYY-MM-DD"
  1081. else:
  1082. self._date_format = date_format
  1083. if datetime_format is None:
  1084. self._datetime_format = "YYYY-MM-DD HH:MM:SS"
  1085. else:
  1086. self._datetime_format = datetime_format
  1087. self._mode = mode
  1088. @property
  1089. def date_format(self) -> str:
  1090. """
  1091. Format string for dates written into Excel files (e.g. 'YYYY-MM-DD').
  1092. """
  1093. return self._date_format
  1094. @property
  1095. def datetime_format(self) -> str:
  1096. """
  1097. Format string for dates written into Excel files (e.g. 'YYYY-MM-DD').
  1098. """
  1099. return self._datetime_format
  1100. @property
  1101. def if_sheet_exists(self) -> str:
  1102. """
  1103. How to behave when writing to a sheet that already exists in append mode.
  1104. """
  1105. return self._if_sheet_exists
  1106. def __fspath__(self) -> str:
  1107. return getattr(self._handles.handle, "name", "")
  1108. def _get_sheet_name(self, sheet_name: str | None) -> str:
  1109. if sheet_name is None:
  1110. sheet_name = self._cur_sheet
  1111. if sheet_name is None: # pragma: no cover
  1112. raise ValueError("Must pass explicit sheet_name or set _cur_sheet property")
  1113. return sheet_name
  1114. def _value_with_fmt(
  1115. self, val
  1116. ) -> tuple[
  1117. int | float | bool | str | datetime.datetime | datetime.date, str | None
  1118. ]:
  1119. """
  1120. Convert numpy types to Python types for the Excel writers.
  1121. Parameters
  1122. ----------
  1123. val : object
  1124. Value to be written into cells
  1125. Returns
  1126. -------
  1127. Tuple with the first element being the converted value and the second
  1128. being an optional format
  1129. """
  1130. fmt = None
  1131. if is_integer(val):
  1132. val = int(val)
  1133. elif is_float(val):
  1134. val = float(val)
  1135. elif is_bool(val):
  1136. val = bool(val)
  1137. elif isinstance(val, datetime.datetime):
  1138. fmt = self._datetime_format
  1139. elif isinstance(val, datetime.date):
  1140. fmt = self._date_format
  1141. elif isinstance(val, datetime.timedelta):
  1142. val = val.total_seconds() / 86400
  1143. fmt = "0"
  1144. else:
  1145. val = str(val)
  1146. return val, fmt
  1147. @classmethod
  1148. def check_extension(cls, ext: str) -> Literal[True]:
  1149. """
  1150. checks that path's extension against the Writer's supported
  1151. extensions. If it isn't supported, raises UnsupportedFiletypeError.
  1152. """
  1153. if ext.startswith("."):
  1154. ext = ext[1:]
  1155. if not any(ext in extension for extension in cls._supported_extensions):
  1156. raise ValueError(f"Invalid extension for engine '{cls.engine}': '{ext}'")
  1157. return True
  1158. # Allow use as a contextmanager
  1159. def __enter__(self) -> Self:
  1160. return self
  1161. def __exit__(
  1162. self,
  1163. exc_type: type[BaseException] | None,
  1164. exc_value: BaseException | None,
  1165. traceback: TracebackType | None,
  1166. ) -> None:
  1167. self.close()
  1168. def close(self) -> None:
  1169. """synonym for save, to make it more file-like"""
  1170. self._save()
  1171. self._handles.close()
  1172. XLS_SIGNATURES = (
  1173. b"\x09\x00\x04\x00\x07\x00\x10\x00", # BIFF2
  1174. b"\x09\x02\x06\x00\x00\x00\x10\x00", # BIFF3
  1175. b"\x09\x04\x06\x00\x00\x00\x10\x00", # BIFF4
  1176. b"\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1", # Compound File Binary
  1177. )
  1178. ZIP_SIGNATURE = b"PK\x03\x04"
  1179. PEEK_SIZE = max(map(len, XLS_SIGNATURES + (ZIP_SIGNATURE,)))
  1180. @doc(storage_options=_shared_docs["storage_options"])
  1181. def inspect_excel_format(
  1182. content_or_path: FilePath | ReadBuffer[bytes],
  1183. storage_options: StorageOptions | None = None,
  1184. ) -> str | None:
  1185. """
  1186. Inspect the path or content of an excel file and get its format.
  1187. Adopted from xlrd: https://github.com/python-excel/xlrd.
  1188. Parameters
  1189. ----------
  1190. content_or_path : str or file-like object
  1191. Path to file or content of file to inspect. May be a URL.
  1192. {storage_options}
  1193. Returns
  1194. -------
  1195. str or None
  1196. Format of file if it can be determined.
  1197. Raises
  1198. ------
  1199. ValueError
  1200. If resulting stream is empty.
  1201. BadZipFile
  1202. If resulting stream does not have an XLS signature and is not a valid zipfile.
  1203. """
  1204. if isinstance(content_or_path, bytes):
  1205. content_or_path = BytesIO(content_or_path)
  1206. with get_handle(
  1207. content_or_path, "rb", storage_options=storage_options, is_text=False
  1208. ) as handle:
  1209. stream = handle.handle
  1210. stream.seek(0)
  1211. buf = stream.read(PEEK_SIZE)
  1212. if buf is None:
  1213. raise ValueError("stream is empty")
  1214. assert isinstance(buf, bytes)
  1215. peek = buf
  1216. stream.seek(0)
  1217. if any(peek.startswith(sig) for sig in XLS_SIGNATURES):
  1218. return "xls"
  1219. elif not peek.startswith(ZIP_SIGNATURE):
  1220. return None
  1221. with zipfile.ZipFile(stream) as zf:
  1222. # Workaround for some third party files that use forward slashes and
  1223. # lower case names.
  1224. component_names = [
  1225. name.replace("\\", "/").lower() for name in zf.namelist()
  1226. ]
  1227. if "xl/workbook.xml" in component_names:
  1228. return "xlsx"
  1229. if "xl/workbook.bin" in component_names:
  1230. return "xlsb"
  1231. if "content.xml" in component_names:
  1232. return "ods"
  1233. return "zip"
  1234. class ExcelFile:
  1235. """
  1236. Class for parsing tabular Excel sheets into DataFrame objects.
  1237. See read_excel for more documentation.
  1238. Parameters
  1239. ----------
  1240. path_or_buffer : str, bytes, path object (pathlib.Path or py._path.local.LocalPath),
  1241. A file-like object, xlrd workbook or openpyxl workbook.
  1242. If a string or path object, expected to be a path to a
  1243. .xls, .xlsx, .xlsb, .xlsm, .odf, .ods, or .odt file.
  1244. engine : str, default None
  1245. If io is not a buffer or path, this must be set to identify io.
  1246. Supported engines: ``xlrd``, ``openpyxl``, ``odf``, ``pyxlsb``, ``calamine``
  1247. Engine compatibility :
  1248. - ``xlrd`` supports old-style Excel files (.xls).
  1249. - ``openpyxl`` supports newer Excel file formats.
  1250. - ``odf`` supports OpenDocument file formats (.odf, .ods, .odt).
  1251. - ``pyxlsb`` supports Binary Excel files.
  1252. - ``calamine`` supports Excel (.xls, .xlsx, .xlsm, .xlsb)
  1253. and OpenDocument (.ods) file formats.
  1254. .. versionchanged:: 1.2.0
  1255. The engine `xlrd <https://xlrd.readthedocs.io/en/latest/>`_
  1256. now only supports old-style ``.xls`` files.
  1257. When ``engine=None``, the following logic will be
  1258. used to determine the engine:
  1259. - If ``path_or_buffer`` is an OpenDocument format (.odf, .ods, .odt),
  1260. then `odf <https://pypi.org/project/odfpy/>`_ will be used.
  1261. - Otherwise if ``path_or_buffer`` is an xls format,
  1262. ``xlrd`` will be used.
  1263. - Otherwise if ``path_or_buffer`` is in xlsb format,
  1264. `pyxlsb <https://pypi.org/project/pyxlsb/>`_ will be used.
  1265. .. versionadded:: 1.3.0
  1266. - Otherwise if `openpyxl <https://pypi.org/project/openpyxl/>`_ is installed,
  1267. then ``openpyxl`` will be used.
  1268. - Otherwise if ``xlrd >= 2.0`` is installed, a ``ValueError`` will be raised.
  1269. .. warning::
  1270. Please do not report issues when using ``xlrd`` to read ``.xlsx`` files.
  1271. This is not supported, switch to using ``openpyxl`` instead.
  1272. engine_kwargs : dict, optional
  1273. Arbitrary keyword arguments passed to excel engine.
  1274. Examples
  1275. --------
  1276. >>> file = pd.ExcelFile('myfile.xlsx') # doctest: +SKIP
  1277. >>> with pd.ExcelFile("myfile.xls") as xls: # doctest: +SKIP
  1278. ... df1 = pd.read_excel(xls, "Sheet1") # doctest: +SKIP
  1279. """
  1280. from pandas.io.excel._calamine import CalamineReader
  1281. from pandas.io.excel._odfreader import ODFReader
  1282. from pandas.io.excel._openpyxl import OpenpyxlReader
  1283. from pandas.io.excel._pyxlsb import PyxlsbReader
  1284. from pandas.io.excel._xlrd import XlrdReader
  1285. _engines: Mapping[str, Any] = {
  1286. "xlrd": XlrdReader,
  1287. "openpyxl": OpenpyxlReader,
  1288. "odf": ODFReader,
  1289. "pyxlsb": PyxlsbReader,
  1290. "calamine": CalamineReader,
  1291. }
  1292. def __init__(
  1293. self,
  1294. path_or_buffer,
  1295. engine: str | None = None,
  1296. storage_options: StorageOptions | None = None,
  1297. engine_kwargs: dict | None = None,
  1298. ) -> None:
  1299. if engine_kwargs is None:
  1300. engine_kwargs = {}
  1301. if engine is not None and engine not in self._engines:
  1302. raise ValueError(f"Unknown engine: {engine}")
  1303. # First argument can also be bytes, so create a buffer
  1304. if isinstance(path_or_buffer, bytes):
  1305. path_or_buffer = BytesIO(path_or_buffer)
  1306. warnings.warn(
  1307. "Passing bytes to 'read_excel' is deprecated and "
  1308. "will be removed in a future version. To read from a "
  1309. "byte string, wrap it in a `BytesIO` object.",
  1310. FutureWarning,
  1311. stacklevel=find_stack_level(),
  1312. )
  1313. # Could be a str, ExcelFile, Book, etc.
  1314. self.io = path_or_buffer
  1315. # Always a string
  1316. self._io = stringify_path(path_or_buffer)
  1317. # Determine xlrd version if installed
  1318. if import_optional_dependency("xlrd", errors="ignore") is None:
  1319. xlrd_version = None
  1320. else:
  1321. import xlrd
  1322. xlrd_version = Version(get_version(xlrd))
  1323. if engine is None:
  1324. # Only determine ext if it is needed
  1325. ext: str | None
  1326. if xlrd_version is not None and isinstance(path_or_buffer, xlrd.Book):
  1327. ext = "xls"
  1328. else:
  1329. ext = inspect_excel_format(
  1330. content_or_path=path_or_buffer, storage_options=storage_options
  1331. )
  1332. if ext is None:
  1333. raise ValueError(
  1334. "Excel file format cannot be determined, you must specify "
  1335. "an engine manually."
  1336. )
  1337. engine = config.get_option(f"io.excel.{ext}.reader", silent=True)
  1338. if engine == "auto":
  1339. engine = get_default_engine(ext, mode="reader")
  1340. assert engine is not None
  1341. self.engine = engine
  1342. self.storage_options = storage_options
  1343. self._reader = self._engines[engine](
  1344. self._io,
  1345. storage_options=storage_options,
  1346. engine_kwargs=engine_kwargs,
  1347. )
  1348. def __fspath__(self):
  1349. return self._io
  1350. def parse(
  1351. self,
  1352. sheet_name: str | int | list[int] | list[str] | None = 0,
  1353. header: int | Sequence[int] | None = 0,
  1354. names: SequenceNotStr[Hashable] | range | None = None,
  1355. index_col: int | Sequence[int] | None = None,
  1356. usecols=None,
  1357. converters=None,
  1358. true_values: Iterable[Hashable] | None = None,
  1359. false_values: Iterable[Hashable] | None = None,
  1360. skiprows: Sequence[int] | int | Callable[[int], object] | None = None,
  1361. nrows: int | None = None,
  1362. na_values=None,
  1363. parse_dates: list | dict | bool = False,
  1364. date_parser: Callable | lib.NoDefault = lib.no_default,
  1365. date_format: str | dict[Hashable, str] | None = None,
  1366. thousands: str | None = None,
  1367. comment: str | None = None,
  1368. skipfooter: int = 0,
  1369. dtype_backend: DtypeBackend | lib.NoDefault = lib.no_default,
  1370. **kwds,
  1371. ) -> DataFrame | dict[str, DataFrame] | dict[int, DataFrame]:
  1372. """
  1373. Parse specified sheet(s) into a DataFrame.
  1374. Equivalent to read_excel(ExcelFile, ...) See the read_excel
  1375. docstring for more info on accepted parameters.
  1376. Returns
  1377. -------
  1378. DataFrame or dict of DataFrames
  1379. DataFrame from the passed in Excel file.
  1380. Examples
  1381. --------
  1382. >>> df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=['A', 'B', 'C'])
  1383. >>> df.to_excel('myfile.xlsx') # doctest: +SKIP
  1384. >>> file = pd.ExcelFile('myfile.xlsx') # doctest: +SKIP
  1385. >>> file.parse() # doctest: +SKIP
  1386. """
  1387. return self._reader.parse(
  1388. sheet_name=sheet_name,
  1389. header=header,
  1390. names=names,
  1391. index_col=index_col,
  1392. usecols=usecols,
  1393. converters=converters,
  1394. true_values=true_values,
  1395. false_values=false_values,
  1396. skiprows=skiprows,
  1397. nrows=nrows,
  1398. na_values=na_values,
  1399. parse_dates=parse_dates,
  1400. date_parser=date_parser,
  1401. date_format=date_format,
  1402. thousands=thousands,
  1403. comment=comment,
  1404. skipfooter=skipfooter,
  1405. dtype_backend=dtype_backend,
  1406. **kwds,
  1407. )
  1408. @property
  1409. def book(self):
  1410. return self._reader.book
  1411. @property
  1412. def sheet_names(self):
  1413. return self._reader.sheet_names
  1414. def close(self) -> None:
  1415. """close io if necessary"""
  1416. self._reader.close()
  1417. def __enter__(self) -> Self:
  1418. return self
  1419. def __exit__(
  1420. self,
  1421. exc_type: type[BaseException] | None,
  1422. exc_value: BaseException | None,
  1423. traceback: TracebackType | None,
  1424. ) -> None:
  1425. self.close()