I use the xlrd module in Python scripts to extract data from Excel workbooks. I created the simple script below to determine the value in a specific cell in a worksheet that is found in most of the workbooks I deal with.
#!/usr/bin/python import xlrd as xl # Desired value is in L33 rowx = 32 # row 33, but 32 because first row is 0 colx = 11 # column L; A column is 0 file_name = raw_input("File: ") workbook = xl.open_workbook(file_name) theSheet = workbook.sheet_by_name("Reference") theValue = theSheet.cell(rowx, colx) print theValue
But, instead of getting the expected value when I ran the script, I saw the error message below:
$ ./sheetvalue.py File: ../CRQ1222539.xlsm Traceback (most recent call last): File "./sheetvalue.py", line 11, in <module> theSheet = workbook.sheet_by_name("Reference") File "/Users/jasmith1/Library/Python/2.7/lib/python/site-packages/xlrd-1.0.0-p y2.7.egg/xlrd/book.py", line 441, in sheet_by_name raise XLRDError('No sheet named <%r>' % sheet_name) xlrd.biffh.XLRDError: No sheet named <'Reference'> $
The value was in a hidden sheet and I didn't realize at first that the
sheet name was
case sensitive. The sheet name was "REFERENCE", but I had used "Reference",
so saw the "No sheet named" error message. When I changed
workbook.sheet_by_name("Reference")
to
workbook.sheet_by_name("REFERENCE")
, I was then able to
print the desired value.
You can use the Python xlrd module to list the worksheets in a workbook.