473,748 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I read Excel file in Python?

How do I read an Excel file in Python?

I have found a package to read excel file, which can be used on any
platform.

http://www.lexicon.net/sjmachin/xlrd.htm
I installed and working on the examples, I found its printing of cell's
contents in a different manner.
>>import xlrd
book=xlrd.ope n_workbook("Cal culation_file.x ls")
book=xlrd.ope n_workbook("tes tbook.xls")
sh=book.sheet _by_index(0)
for row in range(sh.nrows) :
print sh.row(rx)
[text:u'name', text:u'address' , text:u'ph']
[text:u'sudhir', text:u'bangalor e', number:1234.0]
[text:u'vinay', text:u'bangalor e', number:3264.0]

I am bit confused with slicing. help me....
Thank you,
Regards,
Sudhir.

Oct 5 '06 #1
12 6023

kath wrote:
How do I read an Excel file in Python?

I have found a package to read excel file, which can be used on any
platform.
Hi Sudhir,
So far, so good :-)
>
http://www.lexicon.net/sjmachin/xlrd.htm
I installed and working on the examples, I found its printing of cell's
contents in a different manner.
>import xlrd
book=xlrd.open _workbook("Calc ulation_file.xl s")
book=xlrd.open _workbook("test book.xls")
sh=book.sheet_ by_index(0)
for row in range(sh.nrows) :
print sh.row(rx)
[text:u'name', text:u'address' , text:u'ph']
[text:u'sudhir', text:u'bangalor e', number:1234.0]
[text:u'vinay', text:u'bangalor e', number:3264.0]
It helps when asking questions if you copy/paste exactly what is on
your screen;
in this case
print sh.row(rx)
would have given an error; you must have typed
for rx in range.....

A row is returned as a sequence of Cell objects. What you are seeing is
Python automatically doing repr(cell) on each cell in the row. The
Cell.__repr__ method formats it that way for debugging. Here are some
examples from a little test file of mine:
>>import xlrd
bk = xlrd.open_workb ook('sjm1.xls')
sh = bk.sheet_by_ind ex(0)
row0 = sh.row(0)
row0
[text:u'fubar', number:1.0, number:2.0]
>>firstcell = row0[0]
type(firstcel l)
<class 'xlrd.sheet.Cel l'>
>>firstcell.cty pe
1
>># cell type 1 is text
firstcell.val ue
u'fubar'
>>repr(firstcel l)
"text:u'fub ar'"
>
I am bit confused with slicing. help me....
None of the above is anything to do with slicing; is this a 2nd
problem?

Perhaps you are having trouble with this:
>>help(sh.row_s lice)
Help on method row_slice in module xlrd.sheet:

row_slice(self, rowx, start_colx=0, end_colx=None) method of
xlrd.sheet.Shee t instance
##
# Returns a slice of the Cell objects in the given row.
>>>
sh.row_slice(ro wx, lo, hi) gives the same result as sh.row(rowx)[lo:hi]
-- it is provided because the latter would be inefficient for getting a
small slice from a long row.

If you are having trouble with the general concept of slicing, perhaps
you might like to try the Python tutorial. Otherwise, please try to be
a bit more specific about what the confusion is.

HTH, and e-mail me if you prefer ...

Cheers,
John

Oct 5 '06 #2

John Machin wrote:
kath wrote:
How do I read an Excel file in Python?

I have found a package to read excel file, which can be used on any
platform.

Hi Sudhir,
So far, so good :-)

http://www.lexicon.net/sjmachin/xlrd.htm
I installed and working on the examples, I found its printing of cell's
contents in a different manner.
>>import xlrd
>>book=xlrd.ope n_workbook("Cal culation_file.x ls")
>>book=xlrd.ope n_workbook("tes tbook.xls")
>>sh=book.sheet _by_index(0)
>>for row in range(sh.nrows) :
print sh.row(rx)
[text:u'name', text:u'address' , text:u'ph']
[text:u'sudhir', text:u'bangalor e', number:1234.0]
[text:u'vinay', text:u'bangalor e', number:3264.0]

It helps when asking questions if you copy/paste exactly what is on
your screen;
in this case
print sh.row(rx)
would have given an error; you must have typed
for rx in range.....

A row is returned as a sequence of Cell objects. What you are seeing is
Python automatically doing repr(cell) on each cell in the row. The
Cell.__repr__ method formats it that way for debugging. Here are some
examples from a little test file of mine:
>import xlrd
bk = xlrd.open_workb ook('sjm1.xls')
sh = bk.sheet_by_ind ex(0)
row0 = sh.row(0)
row0
[text:u'fubar', number:1.0, number:2.0]
>firstcell = row0[0]
type(firstcell )
<class 'xlrd.sheet.Cel l'>
>firstcell.ctyp e
1
># cell type 1 is text
firstcell.valu e
u'fubar'
>repr(firstcell )
"text:u'fub ar'"

I am bit confused with slicing. help me....
None of the above is anything to do with slicing; is this a 2nd
problem?

Perhaps you are having trouble with this:
>help(sh.row_sl ice)
Help on method row_slice in module xlrd.sheet:

row_slice(self, rowx, start_colx=0, end_colx=None) method of
xlrd.sheet.Shee t instance
##
# Returns a slice of the Cell objects in the given row.
>>

sh.row_slice(ro wx, lo, hi) gives the same result as sh.row(rowx)[lo:hi]
-- it is provided because the latter would be inefficient for getting a
small slice from a long row.

If you are having trouble with the general concept of slicing, perhaps
you might like to try the Python tutorial. Otherwise, please try to be
a bit more specific about what the confusion is.

HTH, and e-mail me if you prefer ...

Cheers,
John

Hi, thanks for the reply. I just took some time reading help file and
came to know to there is nothing do with slicing. But I do have a
problem with date field in the excel file.

the date( 8/9/2006 ) in Excel file, i am getting the value as 38938.0,
which I get when I convert date values to general format in Excel. I
want the actual date value. How do get that?
Thank you.
regards,
Sudhir.

Oct 5 '06 #3
the date( 8/9/2006 ) in Excel file, i am getting the value as 38938.0,
which I get when I convert date values to general format in Excel. I
want the actual date value. How do get that?
38938 appears to be the date in days since 1/1/1900. I'm sure someone
can help you figure out how to convert that to a more useful value.

-Matt

Oct 5 '06 #4
Matimus wrote:
>>the date( 8/9/2006 ) in Excel file, i am getting the value as 38938.0,
which I get when I convert date values to general format in Excel. I
want the actual date value. How do get that?


38938 appears to be the date in days since 1/1/1900. I'm sure someone
can help you figure out how to convert that to a more useful value.
I believe the win32all extension contains functionality to help with
that, thought it's a long time since I even ran PythonWin (sorry, Mark).

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Oct 5 '06 #5
On 5 Oct 2006 10:25:37 -0700, Matimus <mc******@gmail .comwrote:
the date( 8/9/2006 ) in Excel file, i am getting the value as 38938.0,
which I get when I convert date values to general format in Excel. I
want the actual date value. How do get that?

38938 appears to be the date in days since 1/1/1900. I'm sure someone
can help you figure out how to convert that to a more useful value.
>>excel_date = 38938.0
python_date = datetime.date(1 900, 1, 1) +
datetime.timede lta(days=excel_ date)
>>python_date
datetime.date(2 006, 8, 11)

--
Cheers,
Simon B
si***@brunningo nline.net
http://www.brunningonline.net/simon/blog/
Oct 5 '06 #6
On 10/5/06, Simon Brunning <si***@brunning online.netwrote :
On 5 Oct 2006 10:25:37 -0700, Matimus <mc******@gmail .comwrote:
the date( 8/9/2006 ) in Excel file, i am getting the value as 38938.0,
which I get when I convert date values to general format in Excel. I
want the actual date value. How do get that?
38938 appears to be the date in days since 1/1/1900. I'm sure someone
can help you figure out how to convert that to a more useful value.
>excel_date = 38938.0
python_date = datetime.date(1 900, 1, 1) + datetime.timede lta(days=excel_ date)
python_date
datetime.date(2 006, 8, 11)
Err, that's the wrong answer, isn't it? Perhaps it shoud be
datetime.date(1 900, 1, 29)?

--
Cheers,
Simon B
si***@brunningo nline.net
http://www.brunningonline.net/simon/blog/
Oct 5 '06 #7
>>excel_date = 38938.0
>>python_date = datetime.date(1 900, 1, 1) + datetime.timede lta(days=excel_ date)
>>python_date
datetime.date(2 006, 8, 11)

Err, that's the wrong answer, isn't it? Perhaps it shoud be
datetime.date(1 900, 1, 29)?
Actually was about to post same solution and got same results. (BTW
Simon, the OP date is Aug 9th, 2006). Scratched head and googled for
excel date calculations... found this bug where it treats 1900 as leap
year incorrectly:
http://www.ozgrid.com/Excel/ExcelDateandTimes.htm

Plus it treats 1 jan 1900 as day 1, not 0 so just subtract 2 in the
calc:
>>>python_dat e = datetime.date(1 900, 1, 1) + datetime.timede lta(days=excel_ date - 2)
python_date
datetime.date(2 006, 8, 9)

HTH.

Oct 5 '06 #8
ho**********@ya hoo.com wrote:
>excel_date = 38938.0
>python_date = datetime.date(1 900, 1, 1) + datetime.timede lta(days=excel_ date)
>python_date
datetime.date(2 006, 8, 11)
Err, that's the wrong answer, isn't it? Perhaps it shoud be
datetime.date(1 900, 1, 29)?

Actually was about to post same solution and got same results. (BTW
Simon, the OP date is Aug 9th, 2006). Scratched head and googled for
excel date calculations... found this bug where it treats 1900 as leap
year incorrectly:
http://www.ozgrid.com/Excel/ExcelDateandTimes.htm

Plus it treats 1 jan 1900 as day 1, not 0 so just subtract 2 in the
calc:
>>python_date = datetime.date(1 900, 1, 1) + datetime.timede lta(days=excel_ date - 2)
python_date
datetime.date(2 006, 8, 9)
.... and 2006-08-09 is only the correct answer if the spreadsheet was,
when last saved, using the 1900 ("Windows") date system, not the 1904
("Macintosh" ) date system.

All the OP needs to do is to read the documentation that comes with the
xlrd package. It describes the problems with Excel dates, and offers
functions for conversion between the Excel date numbers and (year,
month, day, hour, minute, second) tuples which of course are
interoperable with Python's datetime module and with mx.DateTime.

| >>import xlrd
| >>xlrd.xldate_a s_tuple(38938.0 , 0)
| (2006, 8, 9, 0, 0, 0)
| >>xlrd.xldate_a s_tuple(38938.0 , 1)
| (2010, 8, 10, 0, 0, 0)
| >>>

Cheers,
John

Oct 5 '06 #9
On 5 Oct 2006 12:49:53 -0700, ho**********@ya hoo.com
<ho**********@y ahoo.comwrote:
Actually was about to post same solution and got same results. (BTW
Simon, the OP date is Aug 9th, 2006). Scratched head and googled for
excel date calculations... found this bug where it treats 1900 as leap
year incorrectly:
http://www.ozgrid.com/Excel/ExcelDateandTimes.htm
Ah - I was reading the OP's 8/9/2006 in the European way - DD/MM/YYYY.

One might argue over whether DD/MM/YYYY or MM/DD/YYYY are more
rational, but I find it best to avoid *both* those formats, 'cos they
are so easily confused.

--
Cheers,
Simon B
si***@brunningo nline.net
http://www.brunningonline.net/simon/blog/
Oct 6 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5898
by: dmateer | last post by:
I have the following code in an asp.net page: System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection(); cn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\myservername\ExcelTemp\Temp.xls;Extended Properties=Excel 8.0"; cn.Open(); cn.Close();
0
1258
by: Ashish Tiwari | last post by:
Can i read excel file into asp.net 2.0
1
3506
by: sunnyK | last post by:
hi all !!! i m new here ,,, i m suffering with excel file. i want help regarding how to open excel file in java as file & read row & column wise ..... not database (e.g. using jdbc odbc connectivity...) waiting for reply .... if code is available then ......
0
1319
by: student491 | last post by:
hello, I opened the excel file by using workbook.open() method... Now i want to read the data from the excel file.. & save it into array. help out .. how to do this. Then i want to pass that array through SetSourceData() method & want to draw cunztomized chart... (I am using Excel 2007, C# 2005, COM)
0
1625
by: minhtran | last post by:
Hi Everyone Lately, I found the code as to read all file from Excel file as ".xls" from Excel before 2007, but does not work for ".xlsx" Excel 2007, Please, any help for me (new hired database Developer). A great appreciation from me. Thank you all, the code as below (this code from Internet) TextFilename = Left(TextFilename, Len(TextFilename) - 4) TextFilename += ".txt" If File.Exists(TextFilename) Then ...
1
1370
by: plusplus | last post by:
Hi, thanks for taking up your precious time reading my question (: I am using 'Mircrosoft Visual Studio.NET'. The system have to read in an excel file, then convert to XML file, then lastly output to a textfile. Any pro there pls help. I am real bad at programming~
0
1510
by: paintballer4lfe | last post by:
Hello, i am trying to create a code or get something that lets me read an excel file and take whats in the excel file and then put all of the cells into a list box, or import certain cells into a list box, which ever one will work. So far i havent found anything that has worked. Someone pleas help me with this, this code is over my head. Thank you. By the way, The application is for a Free Student Based Organization called HCHY, its an...
0
1473
by: Manesh Pawar | last post by:
Hello All, I need to read a Excel file to Import that data into the database. I can read it When Excel data Is In Tabular Form, but here, Excel data is in Random cells Format. So, Will you please tell me, how to read excel's cell's value.. Thanking You
2
3338
by: Ehsan arman | last post by:
I was wondering how I could read excel file names in a particular folder using Access VBA. I need to compare the file names in that folder with what I have in a field of Access table.
0
8822
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
9359
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...
1
9310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8235
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6792
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
6072
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();...
1
3298
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
2
2774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.