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
> 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!
Well, I could (and doubtless others will) give you
some working code, but the code you give above must
come from somewhere -- some Microsoft example, or
a VBS repository, perhaps. Since you've already done
the only Python-specific part of the operation,
calling win32com.client.Dispatch, the rest is the
same in Python as in any other language.
However, to get you going...
<code>
import win32com.client
xlApp = win32com.client.gencache.EnsureDispatch ("Excel.Application")
xlWb = xlApp.Workbooks.Open ("c:/temp/temp.xls")
xlSht = xlWb.Worksheets (1)
for row in range (3):
for col in range (3):
print "(%d,%d) => %s" % (row, col, xlSht.Cells (1+row, 1+col).Value)
xlApp.Quit ()
</code>
NB There are several ways to do most of this. You could use,
for example, xlWb.ActiveSheet rather than Worksheets (1) etc.
Just go with whatever works for you.
Also, note that it's often useful to record what you want to
do as a macro within Excel, and then to look at the resulting
VBA to know what Python should be doing to achieve the same
result.
TJG
________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________
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
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/understanding/excel/default.aspx
http://msdn.microsoft.com/office/understanding/excel/documentation/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/
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/search/searchRecipes?query=excel
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
import win32com.client
import win32com.client.dynamic
class Excel:
def __init__(self, filename=None):
self.xlApp =
win32com.client.dynamic.Dispatch('Excel.Application')
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
See http://www.lexicon.net/sjmachin/xlrd.htm or look for xlrd in the
Cheese Shop.