Excel Read
Excel Read
1. To Read/ Write data from XLS file, we need XLRD, XLWT python module (work on any plateform)
2. Download package from http://www.python-excel.org/
3. Unzip downloaded jar file, pick folder "xlrd" & xlwt and place is either in your current directory
or "Lib" folder inside python installation
OR Install python module by following way
a) Unzip downloaded python module
b) Open command prompt and move to location where setup.py file is place(inside unzip folder)
c) Write command on console python setup.py install
READ XLS FILE
1. Import xlrd python module
import xlrd
2. Create workbook object
wk= xlrd.open_workbook("G:\Data.xls")
Here with workbook object we can find number of sheets, sheet name etc
print wk.nsheets
print wk.sheet_names()
3. Now create a sheet object on which you want to perform task
print sh.nrows
4. To work on cell or to read data from cell, create a cell objet
import xlrd
wk= xlrd.open_workbook("G:\Data.xls")
print wk.nsheets
print wk.sheet_names()
sh= wk.sheet_by_index(0);
print sh.nrows
c1 = sh.cell(1,1)
print c1.value
WRITE DATA TO EXCEL FILE
1. Import xlwt python module
import xlwt
import xlwt
wk= xlwt.Workbook()
ws = wk.add_sheet("Sheet1")
ws.write(1,1,"HELLO")
wk.save("G:\N1.xls")