I wanted to know how to read an entire column without iterating from an excel sheet using win32com client for python.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Have you looked into the openpyxl library? From the documentation:
There's also support for iterators and other goodies.
The fastest way would be to use the built in
Range
functionality through thewin32com.client
API. However, I'm not a big fan of it. I think the API is confusing and badly documented, and using it isn't very pythonic (but that's just me).If efficiency is not an issue for you, you can use the excellent xlrd library. Like so:
That gives you the cell objects. To get pure values, use
sheet.col_values
(and there are a few other methods that are real nice to work with).Just remember that xlrd stand for "excel read", so if you want to write to an excel file you need a different library called "xlwt" (which is also pretty good, though less so than xlrd in my opinion).
You can read an entire column without iterating from a sheet using the
Range
collection. You should never useCells
if performacne is any concern. Python uses the win32com module to interact with the Excel COM library. Whenever you use Python and COM (Excel, PowerPoint, Acess, ADODB, etc.) one of your biggest performance constraints will be IO between COM and Python. With theRange
method you only make one COM method call while withCells
you make one for each row. This would also be faster if you were doing the same in VBA or .NETIn the following test I created a worksheet with 10 random characters in cells A1 through A2000. I then extracted these values into lists using both Range and Cells.
In this case Range is 2 orders of magnitude faster (146x) faster than Cells. Note that the Range method returns a 2D list where each inner list is a row. The list iteration transposes
vals
into a 2D list where the inner list is a column.