473,224 Members | 1,861 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,224 software developers and data experts.

How To Read Excel Files In Python?

Hello,

Can I get some help on how to read the excel files using python?

from win32com.client import Dispatch
xlApp = Dispatch("Excel.Application")
xlWb = xlApp.Workbooks.Open("Read.xls")
xlSht = xlWb.WorkSheets(1)

But sadly, I am unable to proceed further about how to read the cells of the
worksheet of my excel file!

Thanks for your help and cooperation.

Best regards,
Anand
Dec 13 '05 #1
6 15902
Anand schrieb:
Hello,

Can I get some help on how to read the excel files using python?
[...]


Besides using the Excel component you could use the pyExcelerator
<http://sourceforge.net/projects/pyexcelerator> module.
You even don't need Windows for it.
Bye,
Dennis
Dec 13 '05 #2
Anand wrote:
Hello,

Can I get some help on how to read the excel files using python?

from win32com.client import Dispatch
xlApp = Dispatch("Excel.Application")
xlWb = xlApp.Workbooks.Open("Read.xls")
xlSht = xlWb.WorkSheets(1)

But sadly, I am unable to proceed further about how to read the cells of the
worksheet of my excel file!

Thanks for your help and cooperation.

Best regards,
Anand

The best way to proceed is to record actions as macros in Excel and then
use the recorded VBA as a guide to the Pythin required.

Unfortunately the office APIs aren't very complete in their documentation,

Good places to look, as long as you don't mind rooting around:

http://msdn.microsoft.com/office/und...l/default.aspx

http://msdn.microsoft.com/office/und...n/default.aspx

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Dec 13 '05 #3

Anand wrote:
Hello,

Can I get some help on how to read the excel files using python?

from win32com.client import Dispatch
xlApp = Dispatch("Excel.Application")
xlWb = xlApp.Workbooks.Open("Read.xls")
xlSht = xlWb.WorkSheets(1)

But sadly, I am unable to proceed further about how to read the cells of the
worksheet of my excel file!

Thanks for your help and cooperation.

Best regards,
Anand


http://www.python.org/pypi/xlrd/0.3a1
and the online (activestate) cookbook has lots of hits, search on
"excel" "MS office" "CSV" "ODBC" etc
http://aspn.activestate.com/ASPN/sea...es?query=excel

Dec 13 '05 #4
Hi!
I had few modif. your code :

import time
from win32com.client import Dispatch
xlApp = Dispatch("Excel.Application")
xlApp.Visible=True
xlWb = xlApp.Workbooks.Open("Read.xls")
print "D3:",xlWb.ActiveSheet.Cells(3,4).Value
time.sleep(2)
xlWb.Close(SaveChanges=0)
xlApp.Quit()

This run OK on my computers.

@-salutations

Michel Claveau

Dec 13 '05 #5
"""Derived from _Python Programming on Win32_ by Mark Hammond and Andy
Robinson"""

import win32com.client
import win32com.client.dynamic

class Excel:
def __init__(self, filename=None):
self.xlApp =
win32com.client.dynamic.Dispatch('Excel.Applicatio n')
if filename:
self.filename = filename
self.xlBook = self.xlApp.Workbooks.Open(filename)
else:
self.xlBook = self.xlApp.Workbooks.Add()
self.filename = ''

def save(self, newfilename=None):
if newfilename:
self.filename = newfilename
self.xlBook.SaveAs(newfilename)
else:
self.xlBook.Save()

def close(self):
self.xlBook.Close(SaveChanges=0)
del self.xlApp

def show(self):
self.xlApp.Visible = 1

def hide(self):
self.xlApp.Visible = 0

def get_cell(self, sheet, row, col):
"get value of one cell"
sht = self.xlBook.Worksheets(sheet)
return sht.Cells(row, col).Value

def set_cell(self, sheet, row, col, value):
"set value of one cell"
sht = self.xlBook.Worksheets(sheet)
sht.Cells(row, col).Value = value

def get_range(self, sheet, row1, col1, row2, col2):
"return a 2d array (i.e. tuple of tuples)"
sht = self.xlBook.Worksheets(sheet)
return sht.Range(sht.Cells(row1, col1), sht.Cells(row2,
col2)).Value

def set_range(self, sheet, leftCol, topRow, data):
bottomRow = topRow + len(data) - 1
rightCol = leftCol + len(data[0]) - 1
sht = self.xlBook.Worksheets(sheet)
sht.Range(sht.Cells(topRow, leftCol), sht.Cells(bottomRow,
rightCol)).Value = data

Dec 13 '05 #6
An alternative: the xlrd module. Don't need Excel on your machine,
don't even need Windows. Pure Python. Happily handles large files
(e.g. 120 Mb). Good date support.

See http://www.lexicon.net/sjmachin/xlrd.htm or look for xlrd in the
Cheese Shop.

Dec 15 '05 #7

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

Similar topics

13
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet...
3
by: ben- | last post by:
I want to import and export Microsoft Excel files. Are there libraries for this ? If someone have advices to give, I'm interested ! -- Mrouze Benot EPITA SIGL Promotion 2005...
6
by: ɽ | last post by:
i want to compare the content in excel,but i don't know whick module to use! can you help me?
10
by: QQ | last post by:
I am trying to read data from Excel files I use the following codes, however, it didn't work Can anyone tell me what the format excel files have, what I should do to read it from C? Thanks a...
4
by: Marco Aschwanden | last post by:
Hi I would like to 1. import an existing Excel-sheet (a template) 2. and add some data to it 3. and save it under a different name afterwards. To me it seems, that pyExcelerator does not...
12
by: kath | last post by:
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...
4
by: michael.pearmain | last post by:
Hi Experts, Looking for a very quick bit on of advice on how to make some python code run. I'm a newbie to both VBA and Python, so i apologise if this is very easy but i'm about to tear my hair...
2
by: wstsoi | last post by:
hi I have to read images from spreadsheet, is it possible to do with php?
1
by: Cupric | last post by:
I have a python script that runs fine from the command line or from within IDLE, but doesn't work through the Vista Task Scheduler. The script downloads some csv files and then uses pywin32 to...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.