By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,799 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,799 IT Pros & Developers. It's quick & easy.

Re: xlrd

P: n/a
here is working code that will read & display contents of all rows & columns in all the sheets, you need xlrd 0.6.1

import xlrd, os, sys

book = xlrd.open_workbook(sys.argv[1])
print "The number of worksheets is", book.nsheets
for shx in range(book.nsheets):
sh = book.sheet_by_index(shx)
print 'tab:%s rows:%s cols:%s ' % (sh.name, sh.nrows, sh.ncols)
for rx in range(sh.nrows):
for cx in range(sh.ncols):
try:
if sh.row_types(rx)[cx] and sh.row_values(rx)[cx]:
print '%4s %s' % (xlrd.cellname(rx, cx), sh.row_values(rx)[cx])
except:
print xlrd.cellname(rx, cx), 'Exception - could not read'
print

-----Original Message-----
From: py************************************************ **@python.org
[mailto:py***************************************** *********@python.org]
On Behalf Of Gary Herron
Sent: Monday, August 04, 2008 5:01 AM
Cc: py*********@python.org
Subject: Re: xlrd
Yeats wrote:
Hi,

Years ago i use xlrd to read data from excel and now I need again, but
i get strange result. The code is:

from xlrd import *

Planilha = open_workbook('C:\\Resultados.xls')
Resultados = Planilha.sheet_by_name('Resultados')
c = (Resultados.cell_value(2,2))
print c

and the result is: 0, but the value in cell is : VERDADEIRO

What´s the problem
I've never used xlrd, but based on other packages for accessing spread
sheets, here's one guess.

Cells can have numeric values or string values. Your cell apparently
has a string, but you are asking for a numeric value, so you get a
zero. Should you be asking for a string value? (That's the way
OpenOffice/python works if I remember correctly.)

Or are you accessing a different cell because you've confused 0-based /
1-based indexing?

Or are you using old outdated versions of xlrd, Python or Excel?

Gary Herron

Thanks and sorry my bad english
Yeats

------------------------------------------------------------------------

--
http://mail.python.org/mailman/listinfo/python-list
--
http://mail.python.org/mailman/listinfo/python-list

The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure. If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof. Thank you.
Aug 4 '08 #1
Share this Question
Share on Google+
1 Reply

P: n/a
On Aug 4, 11:08 pm, Edwin.Mad...@VerizonWireless.com wrote:
here is working code that will read & display contents of all rows & columns in all the sheets, you need xlrd 0.6.1

import xlrd, os, sys

book = xlrd.open_workbook(sys.argv[1])
print "The number of worksheets is", book.nsheets
for shx in range(book.nsheets):
sh = book.sheet_by_index(shx)
print 'tab:%s rows:%s cols:%s ' % (sh.name, sh.nrows, sh.ncols)
for rx in range(sh.nrows):
for cx in range(sh.ncols):
try:
if sh.row_types(rx)[cx] and sh.row_values(rx)[cx]:
Having "and sh.row_values(rx)[cx]" means that it does not "display
contents of all rows & columns in all the sheets"; it omits cells
which contain 0., "", or FALSE.

Consider getting the row_types and the row_values once per row instead
of once per cell.
print '%4s %s' % (xlrd.cellname(rx, cx), sh.row_values(rx)[cx])
If the contents of the cell are a date or an error code, the display
will be rather meaningless.
except:
print xlrd.cellname(rx, cx), 'Exception - could not read'
I'm having difficulty imagining what could go wrong in your try block,
apart from an IndexError (but you are not addressing cells outside the
arena). If something does go wrong, it would be rather drastic, and
very nice to know exactly what the problem is. As well as printing the
cellname, you should get sys.exc_info()[:2] and print the exception
details. But for unexpected exceptions, I'd prefer to print the "where
am I" info and just re-raise the exception, instead of trying to
continue.
print
You can use the supplied script runxlrd.py to get similar information
e.g. on Windows assuming default setup:

prompt>c:\python25\scripts\runxlrd.py show yourfile.xls

For help on displaying dates and error codes, see the get_row_data
function in runxlrd.py, and read the documentation on the Cell class.

Cheers,
John
Aug 4 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.