473,769 Members | 6,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_workb ook(sys.argv[1])
print "The number of worksheets is", book.nsheets
for shx in range(book.nshe ets):
sh = book.sheet_by_i ndex(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(r x)[cx]:
print '%4s %s' % (xlrd.cellname( rx, cx), sh.row_values(r x)[cx])
except:
print xlrd.cellname(r x, 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*********@pyt hon.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('Result ados')
c = (Resultados.cel l_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 4175
On Aug 4, 11:08 pm, Edwin.Mad...@Ve rizonWireless.c om 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_workb ook(sys.argv[1])
print "The number of worksheets is", book.nsheets
for shx in range(book.nshe ets):
sh = book.sheet_by_i ndex(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(r x)[cx]:
Having "and sh.row_values(r x)[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(r x)[cx])
If the contents of the cell are a date or an error code, the display
will be rather meaningless.
except:
print xlrd.cellname(r x, 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:\pytho n25\scripts\run xlrd.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
4120
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 unpickled. Instead of reading a large spreadsheet multiple times, consider pickling it once and loading the saved pickle; can be much faster. * Now works with Python 2.1. Backporting to Python 2.1 was partially
11
2568
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 program pre-maturely. Becasue of this pre-mature termination, the program is not able to execute it's final line of code, which is a very important line. The last line saves the Excel spreadsheet. So is there a way to make sure the last line...
2
2436
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 - http://cheeseshop.python.org/pypi/xlrd/0.5.2 - (to read Excel files) Python windows extensions - http://starship.python.net/crew/mhammond/win32/ - to use ADO wxPython GUI toolkit - http://www.wxpython.org/ - for the GUI interface
2
5008
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, sh_idx = 0, path2 = '//otaam.com/root/SharedFiles/ GlobeOp/Risk/Cal Projects/temp/') File "C:\temp\ReadGoldmanExcelFiles.py", line 36, in excelTocsv book = xlrd.open_workbook(infile) File "C:\Python25\lib\site-packages\xlrd\__init__.py", line 362,...
1
2233
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 uses things out of the xlrd folder, but was curious if it copies files to other locations. Thanks. Jay
1
7866
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
19327
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: import sys import xlrd When I run it, there is an import error saying there is no module named xlrd. However when I type sys.path, the site-packages folder is definitely in the path. Do I somehow need to run the xlrd setup.py
0
282
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 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.)
2
9722
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 discovered that text:u'test1.txt' (XF:22) text:u'test2.txt' (XF:15) text:u'test3.txt' (XF:15) text:u'test4.txt' (XF:15) text:u'test5.txt' (XF:23)
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10222
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10050
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9866
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.