excel.py 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997
  1. """
  2. Module parse to/from Excel
  3. """
  4. # ---------------------------------------------------------------------
  5. # ExcelFile class
  6. import abc
  7. from datetime import date, datetime, time, timedelta
  8. from distutils.version import LooseVersion
  9. from io import UnsupportedOperation
  10. import os
  11. from textwrap import fill
  12. import warnings
  13. import numpy as np
  14. import pandas._libs.json as json
  15. import pandas.compat as compat
  16. from pandas.compat import (
  17. OrderedDict, add_metaclass, lrange, map, range, string_types, u, zip)
  18. from pandas.errors import EmptyDataError
  19. from pandas.util._decorators import Appender, deprecate_kwarg
  20. from pandas.core.dtypes.common import (
  21. is_bool, is_float, is_integer, is_list_like)
  22. from pandas.core import config
  23. from pandas.core.frame import DataFrame
  24. from pandas.io.common import (
  25. _NA_VALUES, _is_url, _stringify_path, _urlopen, _validate_header_arg,
  26. get_filepath_or_buffer)
  27. from pandas.io.formats.printing import pprint_thing
  28. from pandas.io.parsers import TextParser
  29. __all__ = ["read_excel", "ExcelWriter", "ExcelFile"]
  30. _writer_extensions = ["xlsx", "xls", "xlsm"]
  31. _writers = {}
  32. _read_excel_doc = """
  33. Read an Excel file into a pandas DataFrame.
  34. Support both `xls` and `xlsx` file extensions from a local filesystem or URL.
  35. Support an option to read a single sheet or a list of sheets.
  36. Parameters
  37. ----------
  38. io : str, file descriptor, pathlib.Path, ExcelFile or xlrd.Book
  39. The string could be a URL. Valid URL schemes include http, ftp, s3,
  40. gcs, and file. For file URLs, a host is expected. For instance, a local
  41. file could be /path/to/workbook.xlsx.
  42. sheet_name : str, int, list, or None, default 0
  43. Strings are used for sheet names. Integers are used in zero-indexed
  44. sheet positions. Lists of strings/integers are used to request
  45. multiple sheets. Specify None to get all sheets.
  46. Available cases:
  47. * Defaults to ``0``: 1st sheet as a `DataFrame`
  48. * ``1``: 2nd sheet as a `DataFrame`
  49. * ``"Sheet1"``: Load sheet with name "Sheet1"
  50. * ``[0, 1, "Sheet5"]``: Load first, second and sheet named "Sheet5"
  51. as a dict of `DataFrame`
  52. * None: All sheets.
  53. header : int, list of int, default 0
  54. Row (0-indexed) to use for the column labels of the parsed
  55. DataFrame. If a list of integers is passed those row positions will
  56. be combined into a ``MultiIndex``. Use None if there is no header.
  57. names : array-like, default None
  58. List of column names to use. If file contains no header row,
  59. then you should explicitly pass header=None.
  60. index_col : int, list of int, default None
  61. Column (0-indexed) to use as the row labels of the DataFrame.
  62. Pass None if there is no such column. If a list is passed,
  63. those columns will be combined into a ``MultiIndex``. If a
  64. subset of data is selected with ``usecols``, index_col
  65. is based on the subset.
  66. parse_cols : int or list, default None
  67. Alias of `usecols`.
  68. .. deprecated:: 0.21.0
  69. Use `usecols` instead.
  70. usecols : int, str, list-like, or callable default None
  71. Return a subset of the columns.
  72. * If None, then parse all columns.
  73. * If int, then indicates last column to be parsed.
  74. .. deprecated:: 0.24.0
  75. Pass in a list of int instead from 0 to `usecols` inclusive.
  76. * If str, then indicates comma separated list of Excel column letters
  77. and column ranges (e.g. "A:E" or "A,C,E:F"). Ranges are inclusive of
  78. both sides.
  79. * If list of int, then indicates list of column numbers to be parsed.
  80. * If list of string, then indicates list of column names to be parsed.
  81. .. versionadded:: 0.24.0
  82. * If callable, then evaluate each column name against it and parse the
  83. column if the callable returns ``True``.
  84. .. versionadded:: 0.24.0
  85. squeeze : bool, default False
  86. If the parsed data only contains one column then return a Series.
  87. dtype : Type name or dict of column -> type, default None
  88. Data type for data or columns. E.g. {'a': np.float64, 'b': np.int32}
  89. Use `object` to preserve data as stored in Excel and not interpret dtype.
  90. If converters are specified, they will be applied INSTEAD
  91. of dtype conversion.
  92. .. versionadded:: 0.20.0
  93. engine : str, default None
  94. If io is not a buffer or path, this must be set to identify io.
  95. Acceptable values are None or xlrd.
  96. converters : dict, default None
  97. Dict of functions for converting values in certain columns. Keys can
  98. either be integers or column labels, values are functions that take one
  99. input argument, the Excel cell content, and return the transformed
  100. content.
  101. true_values : list, default None
  102. Values to consider as True.
  103. .. versionadded:: 0.19.0
  104. false_values : list, default None
  105. Values to consider as False.
  106. .. versionadded:: 0.19.0
  107. skiprows : list-like
  108. Rows to skip at the beginning (0-indexed).
  109. nrows : int, default None
  110. Number of rows to parse.
  111. .. versionadded:: 0.23.0
  112. na_values : scalar, str, list-like, or dict, default None
  113. Additional strings to recognize as NA/NaN. If dict passed, specific
  114. per-column NA values. By default the following values are interpreted
  115. as NaN: '""" + fill("', '".join(sorted(_NA_VALUES)), 70, subsequent_indent=" ") + """'.
  116. keep_default_na : bool, default True
  117. If na_values are specified and keep_default_na is False the default NaN
  118. values are overridden, otherwise they're appended to.
  119. verbose : bool, default False
  120. Indicate number of NA values placed in non-numeric columns.
  121. parse_dates : bool, list-like, or dict, default False
  122. The behavior is as follows:
  123. * bool. If True -> try parsing the index.
  124. * list of int or names. e.g. If [1, 2, 3] -> try parsing columns 1, 2, 3
  125. each as a separate date column.
  126. * list of lists. e.g. If [[1, 3]] -> combine columns 1 and 3 and parse as
  127. a single date column.
  128. * dict, e.g. {{'foo' : [1, 3]}} -> parse columns 1, 3 as date and call
  129. result 'foo'
  130. If a column or index contains an unparseable date, the entire column or
  131. index will be returned unaltered as an object data type. For non-standard
  132. datetime parsing, use ``pd.to_datetime`` after ``pd.read_csv``
  133. Note: A fast-path exists for iso8601-formatted dates.
  134. date_parser : function, optional
  135. Function to use for converting a sequence of string columns to an array of
  136. datetime instances. The default uses ``dateutil.parser.parser`` to do the
  137. conversion. Pandas will try to call `date_parser` in three different ways,
  138. advancing to the next if an exception occurs: 1) Pass one or more arrays
  139. (as defined by `parse_dates`) as arguments; 2) concatenate (row-wise) the
  140. string values from the columns defined by `parse_dates` into a single array
  141. and pass that; and 3) call `date_parser` once for each row using one or
  142. more strings (corresponding to the columns defined by `parse_dates`) as
  143. arguments.
  144. thousands : str, default None
  145. Thousands separator for parsing string columns to numeric. Note that
  146. this parameter is only necessary for columns stored as TEXT in Excel,
  147. any numeric columns will automatically be parsed, regardless of display
  148. format.
  149. comment : str, default None
  150. Comments out remainder of line. Pass a character or characters to this
  151. argument to indicate comments in the input file. Any data between the
  152. comment string and the end of the current line is ignored.
  153. skip_footer : int, default 0
  154. Alias of `skipfooter`.
  155. .. deprecated:: 0.23.0
  156. Use `skipfooter` instead.
  157. skipfooter : int, default 0
  158. Rows at the end to skip (0-indexed).
  159. convert_float : bool, default True
  160. Convert integral floats to int (i.e., 1.0 --> 1). If False, all numeric
  161. data will be read in as floats: Excel stores all numbers as floats
  162. internally.
  163. mangle_dupe_cols : bool, default True
  164. Duplicate columns will be specified as 'X', 'X.1', ...'X.N', rather than
  165. 'X'...'X'. Passing in False will cause data to be overwritten if there
  166. are duplicate names in the columns.
  167. **kwds : optional
  168. Optional keyword arguments can be passed to ``TextFileReader``.
  169. Returns
  170. -------
  171. DataFrame or dict of DataFrames
  172. DataFrame from the passed in Excel file. See notes in sheet_name
  173. argument for more information on when a dict of DataFrames is returned.
  174. See Also
  175. --------
  176. to_excel : Write DataFrame to an Excel file.
  177. to_csv : Write DataFrame to a comma-separated values (csv) file.
  178. read_csv : Read a comma-separated values (csv) file into DataFrame.
  179. read_fwf : Read a table of fixed-width formatted lines into DataFrame.
  180. Examples
  181. --------
  182. The file can be read using the file name as string or an open file object:
  183. >>> pd.read_excel('tmp.xlsx', index_col=0) # doctest: +SKIP
  184. Name Value
  185. 0 string1 1
  186. 1 string2 2
  187. 2 #Comment 3
  188. >>> pd.read_excel(open('tmp.xlsx', 'rb'),
  189. ... sheet_name='Sheet3') # doctest: +SKIP
  190. Unnamed: 0 Name Value
  191. 0 0 string1 1
  192. 1 1 string2 2
  193. 2 2 #Comment 3
  194. Index and header can be specified via the `index_col` and `header` arguments
  195. >>> pd.read_excel('tmp.xlsx', index_col=None, header=None) # doctest: +SKIP
  196. 0 1 2
  197. 0 NaN Name Value
  198. 1 0.0 string1 1
  199. 2 1.0 string2 2
  200. 3 2.0 #Comment 3
  201. Column types are inferred but can be explicitly specified
  202. >>> pd.read_excel('tmp.xlsx', index_col=0,
  203. ... dtype={'Name': str, 'Value': float}) # doctest: +SKIP
  204. Name Value
  205. 0 string1 1.0
  206. 1 string2 2.0
  207. 2 #Comment 3.0
  208. True, False, and NA values, and thousands separators have defaults,
  209. but can be explicitly specified, too. Supply the values you would like
  210. as strings or lists of strings!
  211. >>> pd.read_excel('tmp.xlsx', index_col=0,
  212. ... na_values=['string1', 'string2']) # doctest: +SKIP
  213. Name Value
  214. 0 NaN 1
  215. 1 NaN 2
  216. 2 #Comment 3
  217. Comment lines in the excel input file can be skipped using the `comment` kwarg
  218. >>> pd.read_excel('tmp.xlsx', index_col=0, comment='#') # doctest: +SKIP
  219. Name Value
  220. 0 string1 1.0
  221. 1 string2 2.0
  222. 2 None NaN
  223. """
  224. def register_writer(klass):
  225. """Adds engine to the excel writer registry. You must use this method to
  226. integrate with ``to_excel``. Also adds config options for any new
  227. ``supported_extensions`` defined on the writer."""
  228. if not compat.callable(klass):
  229. raise ValueError("Can only register callables as engines")
  230. engine_name = klass.engine
  231. _writers[engine_name] = klass
  232. for ext in klass.supported_extensions:
  233. if ext.startswith('.'):
  234. ext = ext[1:]
  235. if ext not in _writer_extensions:
  236. config.register_option("io.excel.{ext}.writer".format(ext=ext),
  237. engine_name, validator=str)
  238. _writer_extensions.append(ext)
  239. def _get_default_writer(ext):
  240. _default_writers = {'xlsx': 'openpyxl', 'xlsm': 'openpyxl', 'xls': 'xlwt'}
  241. try:
  242. import xlsxwriter # noqa
  243. _default_writers['xlsx'] = 'xlsxwriter'
  244. except ImportError:
  245. pass
  246. return _default_writers[ext]
  247. def get_writer(engine_name):
  248. try:
  249. return _writers[engine_name]
  250. except KeyError:
  251. raise ValueError("No Excel writer '{engine}'"
  252. .format(engine=engine_name))
  253. @Appender(_read_excel_doc)
  254. @deprecate_kwarg("parse_cols", "usecols")
  255. @deprecate_kwarg("skip_footer", "skipfooter")
  256. def read_excel(io,
  257. sheet_name=0,
  258. header=0,
  259. names=None,
  260. index_col=None,
  261. parse_cols=None,
  262. usecols=None,
  263. squeeze=False,
  264. dtype=None,
  265. engine=None,
  266. converters=None,
  267. true_values=None,
  268. false_values=None,
  269. skiprows=None,
  270. nrows=None,
  271. na_values=None,
  272. keep_default_na=True,
  273. verbose=False,
  274. parse_dates=False,
  275. date_parser=None,
  276. thousands=None,
  277. comment=None,
  278. skip_footer=0,
  279. skipfooter=0,
  280. convert_float=True,
  281. mangle_dupe_cols=True,
  282. **kwds):
  283. # Can't use _deprecate_kwarg since sheetname=None has a special meaning
  284. if is_integer(sheet_name) and sheet_name == 0 and 'sheetname' in kwds:
  285. warnings.warn("The `sheetname` keyword is deprecated, use "
  286. "`sheet_name` instead", FutureWarning, stacklevel=2)
  287. sheet_name = kwds.pop("sheetname")
  288. if 'sheet' in kwds:
  289. raise TypeError("read_excel() got an unexpected keyword argument "
  290. "`sheet`")
  291. if not isinstance(io, ExcelFile):
  292. io = ExcelFile(io, engine=engine)
  293. return io.parse(
  294. sheet_name=sheet_name,
  295. header=header,
  296. names=names,
  297. index_col=index_col,
  298. usecols=usecols,
  299. squeeze=squeeze,
  300. dtype=dtype,
  301. converters=converters,
  302. true_values=true_values,
  303. false_values=false_values,
  304. skiprows=skiprows,
  305. nrows=nrows,
  306. na_values=na_values,
  307. keep_default_na=keep_default_na,
  308. verbose=verbose,
  309. parse_dates=parse_dates,
  310. date_parser=date_parser,
  311. thousands=thousands,
  312. comment=comment,
  313. skipfooter=skipfooter,
  314. convert_float=convert_float,
  315. mangle_dupe_cols=mangle_dupe_cols,
  316. **kwds)
  317. class _XlrdReader(object):
  318. def __init__(self, filepath_or_buffer):
  319. """Reader using xlrd engine.
  320. Parameters
  321. ----------
  322. filepath_or_buffer : string, path object or Workbook
  323. Object to be parsed.
  324. """
  325. err_msg = "Install xlrd >= 1.0.0 for Excel support"
  326. try:
  327. import xlrd
  328. except ImportError:
  329. raise ImportError(err_msg)
  330. else:
  331. if xlrd.__VERSION__ < LooseVersion("1.0.0"):
  332. raise ImportError(err_msg +
  333. ". Current version " + xlrd.__VERSION__)
  334. # If filepath_or_buffer is a url, want to keep the data as bytes so
  335. # can't pass to get_filepath_or_buffer()
  336. if _is_url(filepath_or_buffer):
  337. filepath_or_buffer = _urlopen(filepath_or_buffer)
  338. elif not isinstance(filepath_or_buffer, (ExcelFile, xlrd.Book)):
  339. filepath_or_buffer, _, _, _ = get_filepath_or_buffer(
  340. filepath_or_buffer)
  341. if isinstance(filepath_or_buffer, xlrd.Book):
  342. self.book = filepath_or_buffer
  343. elif not isinstance(filepath_or_buffer, xlrd.Book) and hasattr(
  344. filepath_or_buffer, "read"):
  345. # N.B. xlrd.Book has a read attribute too
  346. if hasattr(filepath_or_buffer, 'seek'):
  347. try:
  348. # GH 19779
  349. filepath_or_buffer.seek(0)
  350. except UnsupportedOperation:
  351. # HTTPResponse does not support seek()
  352. # GH 20434
  353. pass
  354. data = filepath_or_buffer.read()
  355. self.book = xlrd.open_workbook(file_contents=data)
  356. elif isinstance(filepath_or_buffer, compat.string_types):
  357. self.book = xlrd.open_workbook(filepath_or_buffer)
  358. else:
  359. raise ValueError('Must explicitly set engine if not passing in'
  360. ' buffer or path for io.')
  361. @property
  362. def sheet_names(self):
  363. return self.book.sheet_names()
  364. def parse(self,
  365. sheet_name=0,
  366. header=0,
  367. names=None,
  368. index_col=None,
  369. usecols=None,
  370. squeeze=False,
  371. dtype=None,
  372. true_values=None,
  373. false_values=None,
  374. skiprows=None,
  375. nrows=None,
  376. na_values=None,
  377. verbose=False,
  378. parse_dates=False,
  379. date_parser=None,
  380. thousands=None,
  381. comment=None,
  382. skipfooter=0,
  383. convert_float=True,
  384. mangle_dupe_cols=True,
  385. **kwds):
  386. _validate_header_arg(header)
  387. from xlrd import (xldate, XL_CELL_DATE,
  388. XL_CELL_ERROR, XL_CELL_BOOLEAN,
  389. XL_CELL_NUMBER)
  390. epoch1904 = self.book.datemode
  391. def _parse_cell(cell_contents, cell_typ):
  392. """converts the contents of the cell into a pandas
  393. appropriate object"""
  394. if cell_typ == XL_CELL_DATE:
  395. # Use the newer xlrd datetime handling.
  396. try:
  397. cell_contents = xldate.xldate_as_datetime(
  398. cell_contents, epoch1904)
  399. except OverflowError:
  400. return cell_contents
  401. # Excel doesn't distinguish between dates and time,
  402. # so we treat dates on the epoch as times only.
  403. # Also, Excel supports 1900 and 1904 epochs.
  404. year = (cell_contents.timetuple())[0:3]
  405. if ((not epoch1904 and year == (1899, 12, 31)) or
  406. (epoch1904 and year == (1904, 1, 1))):
  407. cell_contents = time(cell_contents.hour,
  408. cell_contents.minute,
  409. cell_contents.second,
  410. cell_contents.microsecond)
  411. elif cell_typ == XL_CELL_ERROR:
  412. cell_contents = np.nan
  413. elif cell_typ == XL_CELL_BOOLEAN:
  414. cell_contents = bool(cell_contents)
  415. elif convert_float and cell_typ == XL_CELL_NUMBER:
  416. # GH5394 - Excel 'numbers' are always floats
  417. # it's a minimal perf hit and less surprising
  418. val = int(cell_contents)
  419. if val == cell_contents:
  420. cell_contents = val
  421. return cell_contents
  422. ret_dict = False
  423. # Keep sheetname to maintain backwards compatibility.
  424. if isinstance(sheet_name, list):
  425. sheets = sheet_name
  426. ret_dict = True
  427. elif sheet_name is None:
  428. sheets = self.book.sheet_names()
  429. ret_dict = True
  430. else:
  431. sheets = [sheet_name]
  432. # handle same-type duplicates.
  433. sheets = list(OrderedDict.fromkeys(sheets).keys())
  434. output = OrderedDict()
  435. for asheetname in sheets:
  436. if verbose:
  437. print("Reading sheet {sheet}".format(sheet=asheetname))
  438. if isinstance(asheetname, compat.string_types):
  439. sheet = self.book.sheet_by_name(asheetname)
  440. else: # assume an integer if not a string
  441. sheet = self.book.sheet_by_index(asheetname)
  442. data = []
  443. usecols = _maybe_convert_usecols(usecols)
  444. for i in range(sheet.nrows):
  445. row = [_parse_cell(value, typ)
  446. for value, typ in zip(sheet.row_values(i),
  447. sheet.row_types(i))]
  448. data.append(row)
  449. if sheet.nrows == 0:
  450. output[asheetname] = DataFrame()
  451. continue
  452. if is_list_like(header) and len(header) == 1:
  453. header = header[0]
  454. # forward fill and pull out names for MultiIndex column
  455. header_names = None
  456. if header is not None and is_list_like(header):
  457. header_names = []
  458. control_row = [True] * len(data[0])
  459. for row in header:
  460. if is_integer(skiprows):
  461. row += skiprows
  462. data[row], control_row = _fill_mi_header(data[row],
  463. control_row)
  464. if index_col is not None:
  465. header_name, _ = _pop_header_name(data[row], index_col)
  466. header_names.append(header_name)
  467. if is_list_like(index_col):
  468. # Forward fill values for MultiIndex index.
  469. if not is_list_like(header):
  470. offset = 1 + header
  471. else:
  472. offset = 1 + max(header)
  473. # Check if we have an empty dataset
  474. # before trying to collect data.
  475. if offset < len(data):
  476. for col in index_col:
  477. last = data[offset][col]
  478. for row in range(offset + 1, len(data)):
  479. if data[row][col] == '' or data[row][col] is None:
  480. data[row][col] = last
  481. else:
  482. last = data[row][col]
  483. has_index_names = is_list_like(header) and len(header) > 1
  484. # GH 12292 : error when read one empty column from excel file
  485. try:
  486. parser = TextParser(data,
  487. names=names,
  488. header=header,
  489. index_col=index_col,
  490. has_index_names=has_index_names,
  491. squeeze=squeeze,
  492. dtype=dtype,
  493. true_values=true_values,
  494. false_values=false_values,
  495. skiprows=skiprows,
  496. nrows=nrows,
  497. na_values=na_values,
  498. parse_dates=parse_dates,
  499. date_parser=date_parser,
  500. thousands=thousands,
  501. comment=comment,
  502. skipfooter=skipfooter,
  503. usecols=usecols,
  504. mangle_dupe_cols=mangle_dupe_cols,
  505. **kwds)
  506. output[asheetname] = parser.read(nrows=nrows)
  507. if not squeeze or isinstance(output[asheetname], DataFrame):
  508. if header_names:
  509. output[asheetname].columns = output[
  510. asheetname].columns.set_names(header_names)
  511. elif compat.PY2:
  512. output[asheetname].columns = _maybe_convert_to_string(
  513. output[asheetname].columns)
  514. except EmptyDataError:
  515. # No Data, return an empty DataFrame
  516. output[asheetname] = DataFrame()
  517. if ret_dict:
  518. return output
  519. else:
  520. return output[asheetname]
  521. class ExcelFile(object):
  522. """
  523. Class for parsing tabular excel sheets into DataFrame objects.
  524. Uses xlrd. See read_excel for more documentation
  525. Parameters
  526. ----------
  527. io : string, path object (pathlib.Path or py._path.local.LocalPath),
  528. file-like object or xlrd workbook
  529. If a string or path object, expected to be a path to xls or xlsx file.
  530. engine : string, default None
  531. If io is not a buffer or path, this must be set to identify io.
  532. Acceptable values are None or ``xlrd``.
  533. """
  534. _engines = {
  535. 'xlrd': _XlrdReader,
  536. }
  537. def __init__(self, io, engine=None):
  538. if engine is None:
  539. engine = 'xlrd'
  540. if engine not in self._engines:
  541. raise ValueError("Unknown engine: {engine}".format(engine=engine))
  542. # could be a str, ExcelFile, Book, etc.
  543. self.io = io
  544. # Always a string
  545. self._io = _stringify_path(io)
  546. self._reader = self._engines[engine](self._io)
  547. def __fspath__(self):
  548. return self._io
  549. def parse(self,
  550. sheet_name=0,
  551. header=0,
  552. names=None,
  553. index_col=None,
  554. usecols=None,
  555. squeeze=False,
  556. converters=None,
  557. true_values=None,
  558. false_values=None,
  559. skiprows=None,
  560. nrows=None,
  561. na_values=None,
  562. parse_dates=False,
  563. date_parser=None,
  564. thousands=None,
  565. comment=None,
  566. skipfooter=0,
  567. convert_float=True,
  568. mangle_dupe_cols=True,
  569. **kwds):
  570. """
  571. Parse specified sheet(s) into a DataFrame
  572. Equivalent to read_excel(ExcelFile, ...) See the read_excel
  573. docstring for more info on accepted parameters
  574. """
  575. # Can't use _deprecate_kwarg since sheetname=None has a special meaning
  576. if is_integer(sheet_name) and sheet_name == 0 and 'sheetname' in kwds:
  577. warnings.warn("The `sheetname` keyword is deprecated, use "
  578. "`sheet_name` instead", FutureWarning, stacklevel=2)
  579. sheet_name = kwds.pop("sheetname")
  580. elif 'sheetname' in kwds:
  581. raise TypeError("Cannot specify both `sheet_name` "
  582. "and `sheetname`. Use just `sheet_name`")
  583. if 'chunksize' in kwds:
  584. raise NotImplementedError("chunksize keyword of read_excel "
  585. "is not implemented")
  586. return self._reader.parse(sheet_name=sheet_name,
  587. header=header,
  588. names=names,
  589. index_col=index_col,
  590. usecols=usecols,
  591. squeeze=squeeze,
  592. converters=converters,
  593. true_values=true_values,
  594. false_values=false_values,
  595. skiprows=skiprows,
  596. nrows=nrows,
  597. na_values=na_values,
  598. parse_dates=parse_dates,
  599. date_parser=date_parser,
  600. thousands=thousands,
  601. comment=comment,
  602. skipfooter=skipfooter,
  603. convert_float=convert_float,
  604. mangle_dupe_cols=mangle_dupe_cols,
  605. **kwds)
  606. @property
  607. def book(self):
  608. return self._reader.book
  609. @property
  610. def sheet_names(self):
  611. return self._reader.sheet_names
  612. def close(self):
  613. """close io if necessary"""
  614. if hasattr(self.io, 'close'):
  615. self.io.close()
  616. def __enter__(self):
  617. return self
  618. def __exit__(self, exc_type, exc_value, traceback):
  619. self.close()
  620. def _excel2num(x):
  621. """
  622. Convert Excel column name like 'AB' to 0-based column index.
  623. Parameters
  624. ----------
  625. x : str
  626. The Excel column name to convert to a 0-based column index.
  627. Returns
  628. -------
  629. num : int
  630. The column index corresponding to the name.
  631. Raises
  632. ------
  633. ValueError
  634. Part of the Excel column name was invalid.
  635. """
  636. index = 0
  637. for c in x.upper().strip():
  638. cp = ord(c)
  639. if cp < ord("A") or cp > ord("Z"):
  640. raise ValueError("Invalid column name: {x}".format(x=x))
  641. index = index * 26 + cp - ord("A") + 1
  642. return index - 1
  643. def _range2cols(areas):
  644. """
  645. Convert comma separated list of column names and ranges to indices.
  646. Parameters
  647. ----------
  648. areas : str
  649. A string containing a sequence of column ranges (or areas).
  650. Returns
  651. -------
  652. cols : list
  653. A list of 0-based column indices.
  654. Examples
  655. --------
  656. >>> _range2cols('A:E')
  657. [0, 1, 2, 3, 4]
  658. >>> _range2cols('A,C,Z:AB')
  659. [0, 2, 25, 26, 27]
  660. """
  661. cols = []
  662. for rng in areas.split(","):
  663. if ":" in rng:
  664. rng = rng.split(":")
  665. cols.extend(lrange(_excel2num(rng[0]), _excel2num(rng[1]) + 1))
  666. else:
  667. cols.append(_excel2num(rng))
  668. return cols
  669. def _maybe_convert_usecols(usecols):
  670. """
  671. Convert `usecols` into a compatible format for parsing in `parsers.py`.
  672. Parameters
  673. ----------
  674. usecols : object
  675. The use-columns object to potentially convert.
  676. Returns
  677. -------
  678. converted : object
  679. The compatible format of `usecols`.
  680. """
  681. if usecols is None:
  682. return usecols
  683. if is_integer(usecols):
  684. warnings.warn(("Passing in an integer for `usecols` has been "
  685. "deprecated. Please pass in a list of int from "
  686. "0 to `usecols` inclusive instead."),
  687. FutureWarning, stacklevel=2)
  688. return lrange(usecols + 1)
  689. if isinstance(usecols, compat.string_types):
  690. return _range2cols(usecols)
  691. return usecols
  692. def _validate_freeze_panes(freeze_panes):
  693. if freeze_panes is not None:
  694. if (
  695. len(freeze_panes) == 2 and
  696. all(isinstance(item, int) for item in freeze_panes)
  697. ):
  698. return True
  699. raise ValueError("freeze_panes must be of form (row, column)"
  700. " where row and column are integers")
  701. # freeze_panes wasn't specified, return False so it won't be applied
  702. # to output sheet
  703. return False
  704. def _trim_excel_header(row):
  705. # trim header row so auto-index inference works
  706. # xlrd uses '' , openpyxl None
  707. while len(row) > 0 and (row[0] == '' or row[0] is None):
  708. row = row[1:]
  709. return row
  710. def _maybe_convert_to_string(row):
  711. """
  712. Convert elements in a row to string from Unicode.
  713. This is purely a Python 2.x patch and is performed ONLY when all
  714. elements of the row are string-like.
  715. Parameters
  716. ----------
  717. row : array-like
  718. The row of data to convert.
  719. Returns
  720. -------
  721. converted : array-like
  722. """
  723. if compat.PY2:
  724. converted = []
  725. for i in range(len(row)):
  726. if isinstance(row[i], compat.string_types):
  727. try:
  728. converted.append(str(row[i]))
  729. except UnicodeEncodeError:
  730. break
  731. else:
  732. break
  733. else:
  734. row = converted
  735. return row
  736. def _fill_mi_header(row, control_row):
  737. """Forward fills blank entries in row, but only inside the same parent index
  738. Used for creating headers in Multiindex.
  739. Parameters
  740. ----------
  741. row : list
  742. List of items in a single row.
  743. control_row : list of bool
  744. Helps to determine if particular column is in same parent index as the
  745. previous value. Used to stop propagation of empty cells between
  746. different indexes.
  747. Returns
  748. ----------
  749. Returns changed row and control_row
  750. """
  751. last = row[0]
  752. for i in range(1, len(row)):
  753. if not control_row[i]:
  754. last = row[i]
  755. if row[i] == '' or row[i] is None:
  756. row[i] = last
  757. else:
  758. control_row[i] = False
  759. last = row[i]
  760. return _maybe_convert_to_string(row), control_row
  761. # fill blank if index_col not None
  762. def _pop_header_name(row, index_col):
  763. """
  764. Pop the header name for MultiIndex parsing.
  765. Parameters
  766. ----------
  767. row : list
  768. The data row to parse for the header name.
  769. index_col : int, list
  770. The index columns for our data. Assumed to be non-null.
  771. Returns
  772. -------
  773. header_name : str
  774. The extracted header name.
  775. trimmed_row : list
  776. The original data row with the header name removed.
  777. """
  778. # Pop out header name and fill w/blank.
  779. i = index_col if not is_list_like(index_col) else max(index_col)
  780. header_name = row[i]
  781. header_name = None if header_name == "" else header_name
  782. return header_name, row[:i] + [''] + row[i + 1:]
  783. @add_metaclass(abc.ABCMeta)
  784. class ExcelWriter(object):
  785. """
  786. Class for writing DataFrame objects into excel sheets, default is to use
  787. xlwt for xls, openpyxl for xlsx. See DataFrame.to_excel for typical usage.
  788. Parameters
  789. ----------
  790. path : string
  791. Path to xls or xlsx file.
  792. engine : string (optional)
  793. Engine to use for writing. If None, defaults to
  794. ``io.excel.<extension>.writer``. NOTE: can only be passed as a keyword
  795. argument.
  796. date_format : string, default None
  797. Format string for dates written into Excel files (e.g. 'YYYY-MM-DD')
  798. datetime_format : string, default None
  799. Format string for datetime objects written into Excel files
  800. (e.g. 'YYYY-MM-DD HH:MM:SS')
  801. mode : {'w' or 'a'}, default 'w'
  802. File mode to use (write or append).
  803. .. versionadded:: 0.24.0
  804. Attributes
  805. ----------
  806. None
  807. Methods
  808. -------
  809. None
  810. Notes
  811. -----
  812. None of the methods and properties are considered public.
  813. For compatibility with CSV writers, ExcelWriter serializes lists
  814. and dicts to strings before writing.
  815. Examples
  816. --------
  817. Default usage:
  818. >>> with ExcelWriter('path_to_file.xlsx') as writer:
  819. ... df.to_excel(writer)
  820. To write to separate sheets in a single file:
  821. >>> with ExcelWriter('path_to_file.xlsx') as writer:
  822. ... df1.to_excel(writer, sheet_name='Sheet1')
  823. ... df2.to_excel(writer, sheet_name='Sheet2')
  824. You can set the date format or datetime format:
  825. >>> with ExcelWriter('path_to_file.xlsx',
  826. date_format='YYYY-MM-DD',
  827. datetime_format='YYYY-MM-DD HH:MM:SS') as writer:
  828. ... df.to_excel(writer)
  829. You can also append to an existing Excel file:
  830. >>> with ExcelWriter('path_to_file.xlsx', mode='a') as writer:
  831. ... df.to_excel(writer, sheet_name='Sheet3')
  832. """
  833. # Defining an ExcelWriter implementation (see abstract methods for more...)
  834. # - Mandatory
  835. # - ``write_cells(self, cells, sheet_name=None, startrow=0, startcol=0)``
  836. # --> called to write additional DataFrames to disk
  837. # - ``supported_extensions`` (tuple of supported extensions), used to
  838. # check that engine supports the given extension.
  839. # - ``engine`` - string that gives the engine name. Necessary to
  840. # instantiate class directly and bypass ``ExcelWriterMeta`` engine
  841. # lookup.
  842. # - ``save(self)`` --> called to save file to disk
  843. # - Mostly mandatory (i.e. should at least exist)
  844. # - book, cur_sheet, path
  845. # - Optional:
  846. # - ``__init__(self, path, engine=None, **kwargs)`` --> always called
  847. # with path as first argument.
  848. # You also need to register the class with ``register_writer()``.
  849. # Technically, ExcelWriter implementations don't need to subclass
  850. # ExcelWriter.
  851. def __new__(cls, path, engine=None, **kwargs):
  852. # only switch class if generic(ExcelWriter)
  853. if issubclass(cls, ExcelWriter):
  854. if engine is None or (isinstance(engine, string_types) and
  855. engine == 'auto'):
  856. if isinstance(path, string_types):
  857. ext = os.path.splitext(path)[-1][1:]
  858. else:
  859. ext = 'xlsx'
  860. try:
  861. engine = config.get_option('io.excel.{ext}.writer'
  862. .format(ext=ext))
  863. if engine == 'auto':
  864. engine = _get_default_writer(ext)
  865. except KeyError:
  866. error = ValueError("No engine for filetype: '{ext}'"
  867. .format(ext=ext))
  868. raise error
  869. cls = get_writer(engine)
  870. return object.__new__(cls)
  871. # declare external properties you can count on
  872. book = None
  873. curr_sheet = None
  874. path = None
  875. @abc.abstractproperty
  876. def supported_extensions(self):
  877. "extensions that writer engine supports"
  878. pass
  879. @abc.abstractproperty
  880. def engine(self):
  881. "name of engine"
  882. pass
  883. @abc.abstractmethod
  884. def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,
  885. freeze_panes=None):
  886. """
  887. Write given formatted cells into Excel an excel sheet
  888. Parameters
  889. ----------
  890. cells : generator
  891. cell of formatted data to save to Excel sheet
  892. sheet_name : string, default None
  893. Name of Excel sheet, if None, then use self.cur_sheet
  894. startrow : upper left cell row to dump data frame
  895. startcol : upper left cell column to dump data frame
  896. freeze_panes: integer tuple of length 2
  897. contains the bottom-most row and right-most column to freeze
  898. """
  899. pass
  900. @abc.abstractmethod
  901. def save(self):
  902. """
  903. Save workbook to disk.
  904. """
  905. pass
  906. def __init__(self, path, engine=None,
  907. date_format=None, datetime_format=None, mode='w',
  908. **engine_kwargs):
  909. # validate that this engine can handle the extension
  910. if isinstance(path, string_types):
  911. ext = os.path.splitext(path)[-1]
  912. else:
  913. ext = 'xls' if engine == 'xlwt' else 'xlsx'
  914. self.check_extension(ext)
  915. self.path = path
  916. self.sheets = {}
  917. self.cur_sheet = None
  918. if date_format is None:
  919. self.date_format = 'YYYY-MM-DD'
  920. else:
  921. self.date_format = date_format
  922. if datetime_format is None:
  923. self.datetime_format = 'YYYY-MM-DD HH:MM:SS'
  924. else:
  925. self.datetime_format = datetime_format
  926. self.mode = mode
  927. def __fspath__(self):
  928. return _stringify_path(self.path)
  929. def _get_sheet_name(self, sheet_name):
  930. if sheet_name is None:
  931. sheet_name = self.cur_sheet
  932. if sheet_name is None: # pragma: no cover
  933. raise ValueError('Must pass explicit sheet_name or set '
  934. 'cur_sheet property')
  935. return sheet_name
  936. def _value_with_fmt(self, val):
  937. """Convert numpy types to Python types for the Excel writers.
  938. Parameters
  939. ----------
  940. val : object
  941. Value to be written into cells
  942. Returns
  943. -------
  944. Tuple with the first element being the converted value and the second
  945. being an optional format
  946. """
  947. fmt = None
  948. if is_integer(val):
  949. val = int(val)
  950. elif is_float(val):
  951. val = float(val)
  952. elif is_bool(val):
  953. val = bool(val)
  954. elif isinstance(val, datetime):
  955. fmt = self.datetime_format
  956. elif isinstance(val, date):
  957. fmt = self.date_format
  958. elif isinstance(val, timedelta):
  959. val = val.total_seconds() / float(86400)
  960. fmt = '0'
  961. else:
  962. val = compat.to_str(val)
  963. return val, fmt
  964. @classmethod
  965. def check_extension(cls, ext):
  966. """checks that path's extension against the Writer's supported
  967. extensions. If it isn't supported, raises UnsupportedFiletypeError."""
  968. if ext.startswith('.'):
  969. ext = ext[1:]
  970. if not any(ext in extension for extension in cls.supported_extensions):
  971. msg = (u("Invalid extension for engine '{engine}': '{ext}'")
  972. .format(engine=pprint_thing(cls.engine),
  973. ext=pprint_thing(ext)))
  974. raise ValueError(msg)
  975. else:
  976. return True
  977. # Allow use as a contextmanager
  978. def __enter__(self):
  979. return self
  980. def __exit__(self, exc_type, exc_value, traceback):
  981. self.close()
  982. def close(self):
  983. """synonym for save, to make it more file-like"""
  984. return self.save()
  985. class _OpenpyxlWriter(ExcelWriter):
  986. engine = 'openpyxl'
  987. supported_extensions = ('.xlsx', '.xlsm')
  988. def __init__(self, path, engine=None, mode='w', **engine_kwargs):
  989. # Use the openpyxl module as the Excel writer.
  990. from openpyxl.workbook import Workbook
  991. super(_OpenpyxlWriter, self).__init__(path, mode=mode, **engine_kwargs)
  992. if self.mode == 'a': # Load from existing workbook
  993. from openpyxl import load_workbook
  994. book = load_workbook(self.path)
  995. self.book = book
  996. else:
  997. # Create workbook object with default optimized_write=True.
  998. self.book = Workbook()
  999. if self.book.worksheets:
  1000. try:
  1001. self.book.remove(self.book.worksheets[0])
  1002. except AttributeError:
  1003. # compat - for openpyxl <= 2.4
  1004. self.book.remove_sheet(self.book.worksheets[0])
  1005. def save(self):
  1006. """
  1007. Save workbook to disk.
  1008. """
  1009. return self.book.save(self.path)
  1010. @classmethod
  1011. def _convert_to_style(cls, style_dict):
  1012. """
  1013. converts a style_dict to an openpyxl style object
  1014. Parameters
  1015. ----------
  1016. style_dict : style dictionary to convert
  1017. """
  1018. from openpyxl.style import Style
  1019. xls_style = Style()
  1020. for key, value in style_dict.items():
  1021. for nk, nv in value.items():
  1022. if key == "borders":
  1023. (xls_style.borders.__getattribute__(nk)
  1024. .__setattr__('border_style', nv))
  1025. else:
  1026. xls_style.__getattribute__(key).__setattr__(nk, nv)
  1027. return xls_style
  1028. @classmethod
  1029. def _convert_to_style_kwargs(cls, style_dict):
  1030. """
  1031. Convert a style_dict to a set of kwargs suitable for initializing
  1032. or updating-on-copy an openpyxl v2 style object
  1033. Parameters
  1034. ----------
  1035. style_dict : dict
  1036. A dict with zero or more of the following keys (or their synonyms).
  1037. 'font'
  1038. 'fill'
  1039. 'border' ('borders')
  1040. 'alignment'
  1041. 'number_format'
  1042. 'protection'
  1043. Returns
  1044. -------
  1045. style_kwargs : dict
  1046. A dict with the same, normalized keys as ``style_dict`` but each
  1047. value has been replaced with a native openpyxl style object of the
  1048. appropriate class.
  1049. """
  1050. _style_key_map = {
  1051. 'borders': 'border',
  1052. }
  1053. style_kwargs = {}
  1054. for k, v in style_dict.items():
  1055. if k in _style_key_map:
  1056. k = _style_key_map[k]
  1057. _conv_to_x = getattr(cls, '_convert_to_{k}'.format(k=k),
  1058. lambda x: None)
  1059. new_v = _conv_to_x(v)
  1060. if new_v:
  1061. style_kwargs[k] = new_v
  1062. return style_kwargs
  1063. @classmethod
  1064. def _convert_to_color(cls, color_spec):
  1065. """
  1066. Convert ``color_spec`` to an openpyxl v2 Color object
  1067. Parameters
  1068. ----------
  1069. color_spec : str, dict
  1070. A 32-bit ARGB hex string, or a dict with zero or more of the
  1071. following keys.
  1072. 'rgb'
  1073. 'indexed'
  1074. 'auto'
  1075. 'theme'
  1076. 'tint'
  1077. 'index'
  1078. 'type'
  1079. Returns
  1080. -------
  1081. color : openpyxl.styles.Color
  1082. """
  1083. from openpyxl.styles import Color
  1084. if isinstance(color_spec, str):
  1085. return Color(color_spec)
  1086. else:
  1087. return Color(**color_spec)
  1088. @classmethod
  1089. def _convert_to_font(cls, font_dict):
  1090. """
  1091. Convert ``font_dict`` to an openpyxl v2 Font object
  1092. Parameters
  1093. ----------
  1094. font_dict : dict
  1095. A dict with zero or more of the following keys (or their synonyms).
  1096. 'name'
  1097. 'size' ('sz')
  1098. 'bold' ('b')
  1099. 'italic' ('i')
  1100. 'underline' ('u')
  1101. 'strikethrough' ('strike')
  1102. 'color'
  1103. 'vertAlign' ('vertalign')
  1104. 'charset'
  1105. 'scheme'
  1106. 'family'
  1107. 'outline'
  1108. 'shadow'
  1109. 'condense'
  1110. Returns
  1111. -------
  1112. font : openpyxl.styles.Font
  1113. """
  1114. from openpyxl.styles import Font
  1115. _font_key_map = {
  1116. 'sz': 'size',
  1117. 'b': 'bold',
  1118. 'i': 'italic',
  1119. 'u': 'underline',
  1120. 'strike': 'strikethrough',
  1121. 'vertalign': 'vertAlign',
  1122. }
  1123. font_kwargs = {}
  1124. for k, v in font_dict.items():
  1125. if k in _font_key_map:
  1126. k = _font_key_map[k]
  1127. if k == 'color':
  1128. v = cls._convert_to_color(v)
  1129. font_kwargs[k] = v
  1130. return Font(**font_kwargs)
  1131. @classmethod
  1132. def _convert_to_stop(cls, stop_seq):
  1133. """
  1134. Convert ``stop_seq`` to a list of openpyxl v2 Color objects,
  1135. suitable for initializing the ``GradientFill`` ``stop`` parameter.
  1136. Parameters
  1137. ----------
  1138. stop_seq : iterable
  1139. An iterable that yields objects suitable for consumption by
  1140. ``_convert_to_color``.
  1141. Returns
  1142. -------
  1143. stop : list of openpyxl.styles.Color
  1144. """
  1145. return map(cls._convert_to_color, stop_seq)
  1146. @classmethod
  1147. def _convert_to_fill(cls, fill_dict):
  1148. """
  1149. Convert ``fill_dict`` to an openpyxl v2 Fill object
  1150. Parameters
  1151. ----------
  1152. fill_dict : dict
  1153. A dict with one or more of the following keys (or their synonyms),
  1154. 'fill_type' ('patternType', 'patterntype')
  1155. 'start_color' ('fgColor', 'fgcolor')
  1156. 'end_color' ('bgColor', 'bgcolor')
  1157. or one or more of the following keys (or their synonyms).
  1158. 'type' ('fill_type')
  1159. 'degree'
  1160. 'left'
  1161. 'right'
  1162. 'top'
  1163. 'bottom'
  1164. 'stop'
  1165. Returns
  1166. -------
  1167. fill : openpyxl.styles.Fill
  1168. """
  1169. from openpyxl.styles import PatternFill, GradientFill
  1170. _pattern_fill_key_map = {
  1171. 'patternType': 'fill_type',
  1172. 'patterntype': 'fill_type',
  1173. 'fgColor': 'start_color',
  1174. 'fgcolor': 'start_color',
  1175. 'bgColor': 'end_color',
  1176. 'bgcolor': 'end_color',
  1177. }
  1178. _gradient_fill_key_map = {
  1179. 'fill_type': 'type',
  1180. }
  1181. pfill_kwargs = {}
  1182. gfill_kwargs = {}
  1183. for k, v in fill_dict.items():
  1184. pk = gk = None
  1185. if k in _pattern_fill_key_map:
  1186. pk = _pattern_fill_key_map[k]
  1187. if k in _gradient_fill_key_map:
  1188. gk = _gradient_fill_key_map[k]
  1189. if pk in ['start_color', 'end_color']:
  1190. v = cls._convert_to_color(v)
  1191. if gk == 'stop':
  1192. v = cls._convert_to_stop(v)
  1193. if pk:
  1194. pfill_kwargs[pk] = v
  1195. elif gk:
  1196. gfill_kwargs[gk] = v
  1197. else:
  1198. pfill_kwargs[k] = v
  1199. gfill_kwargs[k] = v
  1200. try:
  1201. return PatternFill(**pfill_kwargs)
  1202. except TypeError:
  1203. return GradientFill(**gfill_kwargs)
  1204. @classmethod
  1205. def _convert_to_side(cls, side_spec):
  1206. """
  1207. Convert ``side_spec`` to an openpyxl v2 Side object
  1208. Parameters
  1209. ----------
  1210. side_spec : str, dict
  1211. A string specifying the border style, or a dict with zero or more
  1212. of the following keys (or their synonyms).
  1213. 'style' ('border_style')
  1214. 'color'
  1215. Returns
  1216. -------
  1217. side : openpyxl.styles.Side
  1218. """
  1219. from openpyxl.styles import Side
  1220. _side_key_map = {
  1221. 'border_style': 'style',
  1222. }
  1223. if isinstance(side_spec, str):
  1224. return Side(style=side_spec)
  1225. side_kwargs = {}
  1226. for k, v in side_spec.items():
  1227. if k in _side_key_map:
  1228. k = _side_key_map[k]
  1229. if k == 'color':
  1230. v = cls._convert_to_color(v)
  1231. side_kwargs[k] = v
  1232. return Side(**side_kwargs)
  1233. @classmethod
  1234. def _convert_to_border(cls, border_dict):
  1235. """
  1236. Convert ``border_dict`` to an openpyxl v2 Border object
  1237. Parameters
  1238. ----------
  1239. border_dict : dict
  1240. A dict with zero or more of the following keys (or their synonyms).
  1241. 'left'
  1242. 'right'
  1243. 'top'
  1244. 'bottom'
  1245. 'diagonal'
  1246. 'diagonal_direction'
  1247. 'vertical'
  1248. 'horizontal'
  1249. 'diagonalUp' ('diagonalup')
  1250. 'diagonalDown' ('diagonaldown')
  1251. 'outline'
  1252. Returns
  1253. -------
  1254. border : openpyxl.styles.Border
  1255. """
  1256. from openpyxl.styles import Border
  1257. _border_key_map = {
  1258. 'diagonalup': 'diagonalUp',
  1259. 'diagonaldown': 'diagonalDown',
  1260. }
  1261. border_kwargs = {}
  1262. for k, v in border_dict.items():
  1263. if k in _border_key_map:
  1264. k = _border_key_map[k]
  1265. if k == 'color':
  1266. v = cls._convert_to_color(v)
  1267. if k in ['left', 'right', 'top', 'bottom', 'diagonal']:
  1268. v = cls._convert_to_side(v)
  1269. border_kwargs[k] = v
  1270. return Border(**border_kwargs)
  1271. @classmethod
  1272. def _convert_to_alignment(cls, alignment_dict):
  1273. """
  1274. Convert ``alignment_dict`` to an openpyxl v2 Alignment object
  1275. Parameters
  1276. ----------
  1277. alignment_dict : dict
  1278. A dict with zero or more of the following keys (or their synonyms).
  1279. 'horizontal'
  1280. 'vertical'
  1281. 'text_rotation'
  1282. 'wrap_text'
  1283. 'shrink_to_fit'
  1284. 'indent'
  1285. Returns
  1286. -------
  1287. alignment : openpyxl.styles.Alignment
  1288. """
  1289. from openpyxl.styles import Alignment
  1290. return Alignment(**alignment_dict)
  1291. @classmethod
  1292. def _convert_to_number_format(cls, number_format_dict):
  1293. """
  1294. Convert ``number_format_dict`` to an openpyxl v2.1.0 number format
  1295. initializer.
  1296. Parameters
  1297. ----------
  1298. number_format_dict : dict
  1299. A dict with zero or more of the following keys.
  1300. 'format_code' : str
  1301. Returns
  1302. -------
  1303. number_format : str
  1304. """
  1305. return number_format_dict['format_code']
  1306. @classmethod
  1307. def _convert_to_protection(cls, protection_dict):
  1308. """
  1309. Convert ``protection_dict`` to an openpyxl v2 Protection object.
  1310. Parameters
  1311. ----------
  1312. protection_dict : dict
  1313. A dict with zero or more of the following keys.
  1314. 'locked'
  1315. 'hidden'
  1316. Returns
  1317. -------
  1318. """
  1319. from openpyxl.styles import Protection
  1320. return Protection(**protection_dict)
  1321. def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,
  1322. freeze_panes=None):
  1323. # Write the frame cells using openpyxl.
  1324. sheet_name = self._get_sheet_name(sheet_name)
  1325. _style_cache = {}
  1326. if sheet_name in self.sheets:
  1327. wks = self.sheets[sheet_name]
  1328. else:
  1329. wks = self.book.create_sheet()
  1330. wks.title = sheet_name
  1331. self.sheets[sheet_name] = wks
  1332. if _validate_freeze_panes(freeze_panes):
  1333. wks.freeze_panes = wks.cell(row=freeze_panes[0] + 1,
  1334. column=freeze_panes[1] + 1)
  1335. for cell in cells:
  1336. xcell = wks.cell(
  1337. row=startrow + cell.row + 1,
  1338. column=startcol + cell.col + 1
  1339. )
  1340. xcell.value, fmt = self._value_with_fmt(cell.val)
  1341. if fmt:
  1342. xcell.number_format = fmt
  1343. style_kwargs = {}
  1344. if cell.style:
  1345. key = str(cell.style)
  1346. style_kwargs = _style_cache.get(key)
  1347. if style_kwargs is None:
  1348. style_kwargs = self._convert_to_style_kwargs(cell.style)
  1349. _style_cache[key] = style_kwargs
  1350. if style_kwargs:
  1351. for k, v in style_kwargs.items():
  1352. setattr(xcell, k, v)
  1353. if cell.mergestart is not None and cell.mergeend is not None:
  1354. wks.merge_cells(
  1355. start_row=startrow + cell.row + 1,
  1356. start_column=startcol + cell.col + 1,
  1357. end_column=startcol + cell.mergeend + 1,
  1358. end_row=startrow + cell.mergestart + 1
  1359. )
  1360. # When cells are merged only the top-left cell is preserved
  1361. # The behaviour of the other cells in a merged range is
  1362. # undefined
  1363. if style_kwargs:
  1364. first_row = startrow + cell.row + 1
  1365. last_row = startrow + cell.mergestart + 1
  1366. first_col = startcol + cell.col + 1
  1367. last_col = startcol + cell.mergeend + 1
  1368. for row in range(first_row, last_row + 1):
  1369. for col in range(first_col, last_col + 1):
  1370. if row == first_row and col == first_col:
  1371. # Ignore first cell. It is already handled.
  1372. continue
  1373. xcell = wks.cell(column=col, row=row)
  1374. for k, v in style_kwargs.items():
  1375. setattr(xcell, k, v)
  1376. register_writer(_OpenpyxlWriter)
  1377. class _XlwtWriter(ExcelWriter):
  1378. engine = 'xlwt'
  1379. supported_extensions = ('.xls',)
  1380. def __init__(self, path, engine=None, encoding=None, mode='w',
  1381. **engine_kwargs):
  1382. # Use the xlwt module as the Excel writer.
  1383. import xlwt
  1384. engine_kwargs['engine'] = engine
  1385. if mode == 'a':
  1386. raise ValueError('Append mode is not supported with xlwt!')
  1387. super(_XlwtWriter, self).__init__(path, mode=mode, **engine_kwargs)
  1388. if encoding is None:
  1389. encoding = 'ascii'
  1390. self.book = xlwt.Workbook(encoding=encoding)
  1391. self.fm_datetime = xlwt.easyxf(num_format_str=self.datetime_format)
  1392. self.fm_date = xlwt.easyxf(num_format_str=self.date_format)
  1393. def save(self):
  1394. """
  1395. Save workbook to disk.
  1396. """
  1397. return self.book.save(self.path)
  1398. def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,
  1399. freeze_panes=None):
  1400. # Write the frame cells using xlwt.
  1401. sheet_name = self._get_sheet_name(sheet_name)
  1402. if sheet_name in self.sheets:
  1403. wks = self.sheets[sheet_name]
  1404. else:
  1405. wks = self.book.add_sheet(sheet_name)
  1406. self.sheets[sheet_name] = wks
  1407. if _validate_freeze_panes(freeze_panes):
  1408. wks.set_panes_frozen(True)
  1409. wks.set_horz_split_pos(freeze_panes[0])
  1410. wks.set_vert_split_pos(freeze_panes[1])
  1411. style_dict = {}
  1412. for cell in cells:
  1413. val, fmt = self._value_with_fmt(cell.val)
  1414. stylekey = json.dumps(cell.style)
  1415. if fmt:
  1416. stylekey += fmt
  1417. if stylekey in style_dict:
  1418. style = style_dict[stylekey]
  1419. else:
  1420. style = self._convert_to_style(cell.style, fmt)
  1421. style_dict[stylekey] = style
  1422. if cell.mergestart is not None and cell.mergeend is not None:
  1423. wks.write_merge(startrow + cell.row,
  1424. startrow + cell.mergestart,
  1425. startcol + cell.col,
  1426. startcol + cell.mergeend,
  1427. val, style)
  1428. else:
  1429. wks.write(startrow + cell.row,
  1430. startcol + cell.col,
  1431. val, style)
  1432. @classmethod
  1433. def _style_to_xlwt(cls, item, firstlevel=True, field_sep=',',
  1434. line_sep=';'):
  1435. """helper which recursively generate an xlwt easy style string
  1436. for example:
  1437. hstyle = {"font": {"bold": True},
  1438. "border": {"top": "thin",
  1439. "right": "thin",
  1440. "bottom": "thin",
  1441. "left": "thin"},
  1442. "align": {"horiz": "center"}}
  1443. will be converted to
  1444. font: bold on; \
  1445. border: top thin, right thin, bottom thin, left thin; \
  1446. align: horiz center;
  1447. """
  1448. if hasattr(item, 'items'):
  1449. if firstlevel:
  1450. it = ["{key}: {val}"
  1451. .format(key=key, val=cls._style_to_xlwt(value, False))
  1452. for key, value in item.items()]
  1453. out = "{sep} ".format(sep=(line_sep).join(it))
  1454. return out
  1455. else:
  1456. it = ["{key} {val}"
  1457. .format(key=key, val=cls._style_to_xlwt(value, False))
  1458. for key, value in item.items()]
  1459. out = "{sep} ".format(sep=(field_sep).join(it))
  1460. return out
  1461. else:
  1462. item = "{item}".format(item=item)
  1463. item = item.replace("True", "on")
  1464. item = item.replace("False", "off")
  1465. return item
  1466. @classmethod
  1467. def _convert_to_style(cls, style_dict, num_format_str=None):
  1468. """
  1469. converts a style_dict to an xlwt style object
  1470. Parameters
  1471. ----------
  1472. style_dict : style dictionary to convert
  1473. num_format_str : optional number format string
  1474. """
  1475. import xlwt
  1476. if style_dict:
  1477. xlwt_stylestr = cls._style_to_xlwt(style_dict)
  1478. style = xlwt.easyxf(xlwt_stylestr, field_sep=',', line_sep=';')
  1479. else:
  1480. style = xlwt.XFStyle()
  1481. if num_format_str is not None:
  1482. style.num_format_str = num_format_str
  1483. return style
  1484. register_writer(_XlwtWriter)
  1485. class _XlsxStyler(object):
  1486. # Map from openpyxl-oriented styles to flatter xlsxwriter representation
  1487. # Ordering necessary for both determinism and because some are keyed by
  1488. # prefixes of others.
  1489. STYLE_MAPPING = {
  1490. 'font': [
  1491. (('name',), 'font_name'),
  1492. (('sz',), 'font_size'),
  1493. (('size',), 'font_size'),
  1494. (('color', 'rgb',), 'font_color'),
  1495. (('color',), 'font_color'),
  1496. (('b',), 'bold'),
  1497. (('bold',), 'bold'),
  1498. (('i',), 'italic'),
  1499. (('italic',), 'italic'),
  1500. (('u',), 'underline'),
  1501. (('underline',), 'underline'),
  1502. (('strike',), 'font_strikeout'),
  1503. (('vertAlign',), 'font_script'),
  1504. (('vertalign',), 'font_script'),
  1505. ],
  1506. 'number_format': [
  1507. (('format_code',), 'num_format'),
  1508. ((), 'num_format',),
  1509. ],
  1510. 'protection': [
  1511. (('locked',), 'locked'),
  1512. (('hidden',), 'hidden'),
  1513. ],
  1514. 'alignment': [
  1515. (('horizontal',), 'align'),
  1516. (('vertical',), 'valign'),
  1517. (('text_rotation',), 'rotation'),
  1518. (('wrap_text',), 'text_wrap'),
  1519. (('indent',), 'indent'),
  1520. (('shrink_to_fit',), 'shrink'),
  1521. ],
  1522. 'fill': [
  1523. (('patternType',), 'pattern'),
  1524. (('patterntype',), 'pattern'),
  1525. (('fill_type',), 'pattern'),
  1526. (('start_color', 'rgb',), 'fg_color'),
  1527. (('fgColor', 'rgb',), 'fg_color'),
  1528. (('fgcolor', 'rgb',), 'fg_color'),
  1529. (('start_color',), 'fg_color'),
  1530. (('fgColor',), 'fg_color'),
  1531. (('fgcolor',), 'fg_color'),
  1532. (('end_color', 'rgb',), 'bg_color'),
  1533. (('bgColor', 'rgb',), 'bg_color'),
  1534. (('bgcolor', 'rgb',), 'bg_color'),
  1535. (('end_color',), 'bg_color'),
  1536. (('bgColor',), 'bg_color'),
  1537. (('bgcolor',), 'bg_color'),
  1538. ],
  1539. 'border': [
  1540. (('color', 'rgb',), 'border_color'),
  1541. (('color',), 'border_color'),
  1542. (('style',), 'border'),
  1543. (('top', 'color', 'rgb',), 'top_color'),
  1544. (('top', 'color',), 'top_color'),
  1545. (('top', 'style',), 'top'),
  1546. (('top',), 'top'),
  1547. (('right', 'color', 'rgb',), 'right_color'),
  1548. (('right', 'color',), 'right_color'),
  1549. (('right', 'style',), 'right'),
  1550. (('right',), 'right'),
  1551. (('bottom', 'color', 'rgb',), 'bottom_color'),
  1552. (('bottom', 'color',), 'bottom_color'),
  1553. (('bottom', 'style',), 'bottom'),
  1554. (('bottom',), 'bottom'),
  1555. (('left', 'color', 'rgb',), 'left_color'),
  1556. (('left', 'color',), 'left_color'),
  1557. (('left', 'style',), 'left'),
  1558. (('left',), 'left'),
  1559. ],
  1560. }
  1561. @classmethod
  1562. def convert(cls, style_dict, num_format_str=None):
  1563. """
  1564. converts a style_dict to an xlsxwriter format dict
  1565. Parameters
  1566. ----------
  1567. style_dict : style dictionary to convert
  1568. num_format_str : optional number format string
  1569. """
  1570. # Create a XlsxWriter format object.
  1571. props = {}
  1572. if num_format_str is not None:
  1573. props['num_format'] = num_format_str
  1574. if style_dict is None:
  1575. return props
  1576. if 'borders' in style_dict:
  1577. style_dict = style_dict.copy()
  1578. style_dict['border'] = style_dict.pop('borders')
  1579. for style_group_key, style_group in style_dict.items():
  1580. for src, dst in cls.STYLE_MAPPING.get(style_group_key, []):
  1581. # src is a sequence of keys into a nested dict
  1582. # dst is a flat key
  1583. if dst in props:
  1584. continue
  1585. v = style_group
  1586. for k in src:
  1587. try:
  1588. v = v[k]
  1589. except (KeyError, TypeError):
  1590. break
  1591. else:
  1592. props[dst] = v
  1593. if isinstance(props.get('pattern'), string_types):
  1594. # TODO: support other fill patterns
  1595. props['pattern'] = 0 if props['pattern'] == 'none' else 1
  1596. for k in ['border', 'top', 'right', 'bottom', 'left']:
  1597. if isinstance(props.get(k), string_types):
  1598. try:
  1599. props[k] = ['none', 'thin', 'medium', 'dashed', 'dotted',
  1600. 'thick', 'double', 'hair', 'mediumDashed',
  1601. 'dashDot', 'mediumDashDot', 'dashDotDot',
  1602. 'mediumDashDotDot',
  1603. 'slantDashDot'].index(props[k])
  1604. except ValueError:
  1605. props[k] = 2
  1606. if isinstance(props.get('font_script'), string_types):
  1607. props['font_script'] = ['baseline', 'superscript',
  1608. 'subscript'].index(props['font_script'])
  1609. if isinstance(props.get('underline'), string_types):
  1610. props['underline'] = {'none': 0, 'single': 1, 'double': 2,
  1611. 'singleAccounting': 33,
  1612. 'doubleAccounting': 34}[props['underline']]
  1613. return props
  1614. class _XlsxWriter(ExcelWriter):
  1615. engine = 'xlsxwriter'
  1616. supported_extensions = ('.xlsx',)
  1617. def __init__(self, path, engine=None,
  1618. date_format=None, datetime_format=None, mode='w',
  1619. **engine_kwargs):
  1620. # Use the xlsxwriter module as the Excel writer.
  1621. import xlsxwriter
  1622. if mode == 'a':
  1623. raise ValueError('Append mode is not supported with xlsxwriter!')
  1624. super(_XlsxWriter, self).__init__(path, engine=engine,
  1625. date_format=date_format,
  1626. datetime_format=datetime_format,
  1627. mode=mode,
  1628. **engine_kwargs)
  1629. self.book = xlsxwriter.Workbook(path, **engine_kwargs)
  1630. def save(self):
  1631. """
  1632. Save workbook to disk.
  1633. """
  1634. return self.book.close()
  1635. def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,
  1636. freeze_panes=None):
  1637. # Write the frame cells using xlsxwriter.
  1638. sheet_name = self._get_sheet_name(sheet_name)
  1639. if sheet_name in self.sheets:
  1640. wks = self.sheets[sheet_name]
  1641. else:
  1642. wks = self.book.add_worksheet(sheet_name)
  1643. self.sheets[sheet_name] = wks
  1644. style_dict = {'null': None}
  1645. if _validate_freeze_panes(freeze_panes):
  1646. wks.freeze_panes(*(freeze_panes))
  1647. for cell in cells:
  1648. val, fmt = self._value_with_fmt(cell.val)
  1649. stylekey = json.dumps(cell.style)
  1650. if fmt:
  1651. stylekey += fmt
  1652. if stylekey in style_dict:
  1653. style = style_dict[stylekey]
  1654. else:
  1655. style = self.book.add_format(
  1656. _XlsxStyler.convert(cell.style, fmt))
  1657. style_dict[stylekey] = style
  1658. if cell.mergestart is not None and cell.mergeend is not None:
  1659. wks.merge_range(startrow + cell.row,
  1660. startcol + cell.col,
  1661. startrow + cell.mergestart,
  1662. startcol + cell.mergeend,
  1663. cell.val, style)
  1664. else:
  1665. wks.write(startrow + cell.row,
  1666. startcol + cell.col,
  1667. val, style)
  1668. register_writer(_XlsxWriter)