5

I am new to Python and ArcPy, and wish to create a short script that pulls in a value from one specific Excel worksheet cell (namely, A2) and then performs an 'if' statement, whereby if the value of A2 is greater than 1, then a certain text string is returned, but if it is less than or equal to 1, then a different text string is returned.

So far, my code looks like this:

import arcpy
from arcpy import env
arcpy.env.workspace = r"C:\MMO_Model_Test\testdata.xlsx"
inputData = "data"

I have defined my workspace, and set the relevant worksheet as a variable. I now need to reference the cell 'A2' as part of an 'if' statement. This is probably simple for someone who knows how, but I haven't managed to find a relevant example online.

1 Answer 1

7

You'll need to use the xlrd module to read Microsoft Excel files (I think it's limited to .xls files only). To open your spreadsheet and read cell A2, you would use the following:

import xlrd
book = xlrd.open_workbook(r"C:\MMO_Model_Test\testdata.xlsx") # may not work with .xlsx file
sh = book.sheet_by_index(0) # opens the first worksheet
val = sh.cell_value(1,0) # index row,col values corresponding to cell A2
# do something with val

Documentation for the xlrd module is found here.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.