472,961 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,961 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 4106
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.