473,395 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Re: xlrd

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
1 4145
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: John Machin | last post by:
I am pleased to announce a new general release (0.5.2) of xlrd, a Python package for extracting data from Microsoft Excel spreadsheets. CHANGES: * Book and sheet objects can now be pickled and...
11
by: Tempo | last post by:
Hello. I am getting the error that is displayed below, and I know exactly why it occurs. I posted some of my program's code below, and if you look at it you will see that the error terminates the...
2
by: BrendanC | last post by:
I've started learninhg Python and have developed a small Python app that imports Excel data into an Access mdb/jet database. This application has dependencies on the following: XLRD -...
2
by: kylancal | last post by:
I am trying to read an Excel book with XLRD and I am getting the following error Traceback (most recent call last): File "C:\temp\ReadGoldmanExcelFiles.py", line 62, in <module> startRow = 0,...
1
by: JYOUNG79 | last post by:
When running 'python setup.py install' to install items for xlrd to work, does anybody know what items are installed and where items are installed at on a Mac (OS 10.4)? I'm assuming it mainly...
1
by: Krishna | last post by:
I want to delete some rows (by creating a loop may be) using xlrd. Is this possible, if not how do I do that with python? Please help Thanks Krishna
3
by: Chanman | last post by:
This is probably a simple question to most of you, but here goes. I've downloaded the xlrd (version 0.6.1) module and placed in in the site-packages folder. Now, when I write a script, I type: ...
0
by: Gary Herron | last post by:
Yeats wrote: 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...
2
by: patrick.waldo | last post by:
Hi all, I am trying to figure out a way to read colors with xlrd, but I did not understand the formatting.py module. Basically, I want to sort rows that are red or green. My initial attempt...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.