pythonwin32com

Excel using win32com and python


I wanted to know how to read an entire column without iterating from an excel sheet using win32com client for python.


Solution

  • The fastest way would be to use the built in Range functionality through the win32com.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:

    import xlrd
    book = xlrd.open_workbooks('Book1')
    sheet = book.sheet_by_name('Sheet1')
    sheel.col(1)
    sheet.col(2)
    # and so on...
    

    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).