#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Post Reading single cells in excel using Python


    Hi,
    I am trying to read single cells in excel using python. I am not interested in reading the entire sheet at once just one cell at a time.
    Basically I want to be able to read say Cell A1, store it in a variable and B1 store it in a variable.
    If both cells can be read at once and stored in variables that would be better.

    Here is what my code looks like:

    import xlrd
    wb = xlrd.open_workbook("Table.xls")
    sh1 = wb.sheet_by_index(0)

    print "content of", sh1.name
    for rownum in range(sh1.nrows):
    print sh1.row_values(rownum)

    this returns the entire sheet like this:

    [u'GIS_ECA_ALT_AB_BF_ACCESS_A', u'TRN_ACCESS_ALT_A']
    [u'GIS_ECA_ALT_AB_BF_ACCESS_L', u'TRN_ACCESS_ALT_L']
    [u'GIS_ECA_ALT_AB_BF_AIR_WEAPONS_RANGE_A', u'AIR_WEAPONS_RANGE_ALT_A']
    [u'GIS_ECA_ALT_AB_BF_ATS_A', u'GRD_ATS_ALT_A']
    [u'GIS_ECA_ALT_AB_BF_ATS_L', u'GRD_ATS_ALT_L']
    [u'GIS_ECA_ALT_AB_BF_CITY_A', u'ADM_CITY_ALT_A']
    [u'GIS_ECA_ALT_AB_BF_CONTOUR_INDEX_L', u'GNG_CONTOUR_INDEX_ALT_L']
    [u'GIS_ECA_ALT_AB_BF_CONTOUR_L', u'GNG_CONTOUR_ALT_L']
    [u'GIS_ECA_ALT_AB_BF_CONTOUR_T', u'GNG_CONTOUR_ALT_T']

    all I want to be able to do is save the first part of line 1 into a separate variable and do the same for the second part.

    The rest of the rows in the spread sheet will be read using a loop.
    Please note this is just a part of an entire syntax which takes the information from here and applies in other processes.

    I hope I've given enough info. Any help is appreciated.
    Thanks
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    12
    Rep Power
    0
    hi
    every row is being returned as a list. All you would need is to iterate over the elements of that list. Is that what you need ?

    Warm Regards
    Sachin
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    39
    Rep Power
    2

    Use cell


    You should be able to get a single cell using the cell method passing in the row and column number you want. This returns a Cell object which has a value property.

    As an example the following code should print out the value of A1

    Code:
    import xlrd
    wb = xlrd.open_workbook("Table.xls")
    sh1 = wb.sheet_by_index(0)
    print sh1.cell(0,0).value
    There is a fairly good tutorial on the python-excel website.

IMN logo majestic logo threadwatch logo seochat tools logo