B t `@spdZy ddlZWnek r$YnXyeWnddZYnXGdddeZddZGd d d eZdS) z This modules provides a lightweight API to access Excel data. There are many ways to read Excel data, including ODBC. This module uses ADODB and has the advantage of only requiring a file name and a sheet name (no setup required). NcCs|S)N)xrr8/tmp/pip-unpacked-wheel-cqckmaqz/pyutilib/excel/excel.pyunicodesrc@s:eZdZdZddZddZd ddZd d Zd d ZdS) ExcelDocumentz* Represents an opened Excel document. cCs*tjd|_|jdd|ddS)NzADODB.Connectionz!PROVIDER=Microsoft.Jet.OLEDB.4.0;zDATA SOURCE=%sz-;Extended Properties="Excel 8.0;HDR=1;IMEX=1")win32comclientDispatch connectionOpen)selffilenamerrr__init__#s zExcelDocument.__init__cCsDg}|jd}x$|js4||jdj|qW|~|S)zI Returns a list of the name of the sheets found in the document. )r Z OpenSchemaEOFappendFieldsValueMoveNextClose)r result recordsetrrrsheets)s  zExcelDocument.sheetsNcCst||||S)a Returns a sheet object by name. Use sheets() to obtain a list of valid names. encoding is a character encoding name which is used to encode the unicode strings returned by Excel, so that you get plain Python strings. ) _ExcelSheet)r nameencodingorder_byrrrsheet5szExcelDocument.sheetcCs |dS)N)close)r rrr__del__=szExcelDocument.__del__cCs$y|j|`Wn YnXdS)zg Closes the Excel document. It is automatically called when the object is deleted. N)r r)r rrrr@s  zExcelDocument.close)NN) __name__ __module__ __qualname____doc__rrrr rrrrrrs  rcCs&t|tr"|}t|dkr"dS|S)z] Strip the input value if it is a string and returns None if it had only whitespaces rN) isinstance basestringstriplen)valuerrr_stripKs   r*c@s:eZdZdZddZddZddZdd Zdd d Zd S)rzy Represents an Excel sheet from a document, gives methods to obtain column names and iterate on its content. cs4||_||_||_r*fdd}||_nt|_dS)Ncs^t|tr.|}t|dkr"dS|Sn,t|trV|}t|dkrPdS|Sn|SdS)Nr)r%rr'r(encodestr)r))rrrencoder`s     z%_ExcelSheet.__init__..encoder)documentrrrr*)r r.rrrr-r)rrrZs z_ExcelSheet.__init__csPtjd}|tdjjjddzfdd|jDS| ~XdS)z7 Returns a list of column names for the sheet. zADODB.RecordsetzSELECT * FROM [%s]rcsg|]}|jqSr)rName).0field)r rr |sz'_ExcelSheet.columns..N) rrr r rrr.r rr)r rr)r rcolumnsts z_ExcelSheet.columnscCs|S)z; Returns a paged iterator by default. See paged(). )paged)r rrr__iter__sz_ExcelSheet.__iter__ccstjd}|jr8|td|j|jf|jjddn|td|j|jjddyTxD|j si}x&|j D]}| |j || |j <qlW|V|qZW|~Wn|~YnXdS)z Returns an iterator on the data contained in the sheet. Each row is returned as a dictionary with row headers as keys. zADODB.RecordsetzSELECT * FROM [%s] ORDER BY %srr/zSELECT * FROM [%s]N)rrr rr rrr.r rrrrr0rr)r rsourcer2rrrnaives,    z_ExcelSheet.naivec#stjd}jr8|tdjjfjjddn|tdjjjddypfdd|j D}d}xR|rt | |}|j r| d }d }x$|D]}tt |tj|VqWqrWWn |d k r| ~YnXd S) ah Returns an iterator on the data contained in the sheet. Each row is returned as a dictionary with row headers as keys. pagesize is the size of the buffer of rows ; it is an implementation detail but could have an impact on the speed of the iterator. Use pagesize=-1 to buffer the whole sheet in memory. zADODB.RecordsetzSELECT * FROM [%s] ORDER BY %srr/zSELECT * FROM [%s]csg|]}|jqSr)rr0)r1r2)r rrr3sz%_ExcelSheet.paged..TNF)rrr rr rrr.r rzipZGetRowsrrdictmapr)r Zpagesizerfieldsokrowsrowr)r rr5s0   $z_ExcelSheet.pagedN)r9) r!r"r#r$rr4r6r8r5rrrrrUs  r) r$Zwin32com.clientr ImportErrorrobjectrr*rrrrrs ,