xlrd 연습

project/python 2007. 8. 25. 18:41

주어진 엑셀 파일에서 종목코드 및 특정 셀의 값을 읽어서 매개변수로 사용하는 방법

# coding: utf-8
# *-* coding: utf-8 -*-
# -- coding cp949 --
import xlrd, time
book = xlrd.open_workbook("C:\Python25\getting_started\myfile.xls")
print "The number of worksheets is", book.nsheets   #sheet의 갯수

print "Worksheet name(s):", book.sheet_names()

sh = book.sheet_by_index(0)
#sh = book.sheet_by_name("test")    #index로만 하면 sheet의 이름을 마음대로 쓰지 못하니까 이것으로 하는 것이 더 좋다
#print sh.cell_type(0, 0)
#print sh.cell_type(0, 1)
#print sh.cell_type(0, 2)
#print sh.cell_type(1, 0)
#print sh.cell_type(1, 1)
#print sh.cell_type(1, 2)
#print sh.cell_type(2, 0)
#print sh.cell_type(2, 1)
#print sh.cell_type(2, 2)

print "_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/"

#print sh.cell_value(0, 0)
#print sh.cell_value(0, 1)
#print sh.cell_value(0, 2)
#print sh.cell_value(1, 0)
#print sh.cell_value(1, 1)
#print sh.cell_value(1, 2)
#print sh.cell_value(2, 0)
#print sh.cell_value(2, 1)
#print sh.cell_value(2, 2)

print "_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/"

#print sh.col(0)
print "_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/"

#print sh.col_slice(0, start_rowx=1, end_rowx=100)

#print sh.name, sh.nrows, sh.ncols
#print sh.row(2)
#print sh.col(0)
startcol = 7
endcol = 10
print sh.col_slice(0, startcol, endcol)

sh2 = book.sheet_by_index(1)
start = int(sh2.cell_value(0, 0))   #읽어오는 셀의 값에 int()를 씌워버렸더니 그냥 integer로 인식
end = int(sh2.cell_value(1, 0))   #읽어오는 셀의 값에 int()를 씌워버렸더니 그냥 integer로 인식
#print start
#print end
print sh.col_slice(0, start, end)

#print sh.row_types(2)
#print sh.row_values(2)
print "Cell D30 is", sh.cell_value(rowx=29, colx=3)
t = time.time()

#for rx in range(sh.nrows):
#    print sh.row(rx)
#for rx in range(sh.ncols):
#    print sh.col(rx)
print 'Python Elapsed %.02f' % (time.time() - t)

Posted by trigger
,