junbjn98

read excel python

May 14th, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. # In your python terminal cd into the directory having the excel file.
  2. import xlrd
  3. book = xlrd.open_workbook("excel.xls") # in my case the directory contains the excel file named excel.xls
  4.  
  5. # Now to print the number of worksheets in the excel file
  6. print "The number of worksheets are ", book.nsheets
  7.  
  8. # Now the names of the worksheets in excel file
  9. print "The names of worksheets are", book.sheet_names() # returns an array of names
  10.  
  11. # Choose a specific workbook to import data
  12. sheet = book.sheet_by_index(0)
  13.  
  14. # viola you have it
  15. # Now lets say in my excel sheet data starts with rows = 1 to 3 , and columns =0 to 2
  16. # PS the first row are the titles
  17.  
  18. for j in range(0,3):
  19.    for i in range(1,4):
  20.        print "%d" %sheet.cell_value(i,j)
Advertisement
Add Comment
Please, Sign In to add comment