python - Reading Excel sheet with xlrd (xlrd를 이용한 파이썬 엑셀 파일 읽기)

1. xlrd

시작에 앞서 python xlrd패키지를 설치해야 합니다.
https://pypi.python.org/pypi/xlrd/0.7.9

xlrd와 xlwd 두개의 패키지가 존재하는데, xlrd는 read용 패키지 xlwd는 write용 패키지 입니다.

먼저 포스팅에 사용할 엑셀 Sheet은 아래와 같이 작성됐습니다.

Sheet1

Sheet2









2. Open the Excel files and sheets


__author__ = 'Administrator'
import xlrd
wb = xlrd.open_workbook("C:\\test.xlsx")
ws = wb.sheet_by_index(0)
ncol = ws.ncols
nlow = ws.nrows
print "-------- Sheet1 --------"
print "Number of col: " + str(ncol)
print "Number of low: " + str(nlow)
ws = wb.sheet_by_index(1)
ncol = ws.ncols
nlow = ws.nrows
print "-------- Sheet2 --------"
print "Number of col: " + str(ncol)
print "Number of low: " + str(nlow)
view raw gistfile1.txt hosted with ❤ by GitHub
위와 같이 xlrd를 import 해줍니다.

Lien 4 - xlrd.open_workbook()을 통해 Excel파일을 읽어옵니다.

Line 5, 13  - sheet_by_index()를 통해 line 5에선 Sheet1 line 13에선 Sheet2를 각각 열람 할 수 있습니다.

Line 6 - ncols를 통해 열람한 Sheet의 컬럼의 갯수를 확인 할 수 있습니다.

Line 7 - nrows를 통해 열람한 Sheet의 로우의 갯수를 확인 할 수 있습니다.

출력결과 아래와 같습니다.

-------- Sheet1 --------
Number of col: 4
Number of low: 4
-------- Sheet2 --------
Number of col: 2
Number of low: 2

3. Get values in excel file


__author__ = 'Administrator'
import xlrd
wb = xlrd.open_workbook("C:\\test.xlsx")
ws = wb.sheet_by_index(0)
ncol = ws.ncols
nlow = ws.nrows
print "-------- Sheet1 --------"
print "Number of col: " + str(ncol)
print "Number of low: " + str(nlow)
print "-------- Values of low --------"
i = 0
while i < nlow:
print str(ws.row_values(i))
i += 1
print "-------- Values of col --------"
i = 0
while i < ncol:
print str(ws.col_values(i))
i += 1
view raw gistfile1.txt hosted with ❤ by GitHub
row_values와 col_values를 통해 각 각 row와 col의 값을 구할 수 있습니다.

row_values(0)을 이용하면 엑셀의 1번 줄 [1A, 1B, 1C, 1D]를 출력 할 수 있습니다.

col_values(0)을 이용하면 엑셀의 A컬럼 [1A, 2A, 3A, 4A]를 출력 할 수 있습니다.

출력결과 아래와 같습니다.

-------- Sheet1 --------
Number of col: 4
Number of low: 4
-------- Values of low --------
[u'1A', u'1B', u'1C', u'1D']
[u'2A', u'2B', u'2C', u'2D']
[u'3A', u'3B', u'3C', u'3D']
[u'4A', u'4B', u'4C', u'4D']
-------- Values of col --------
[u'1A', u'2A', u'3A', u'4A']
[u'1B', u'2B', u'3B', u'4B']
[u'1C', u'2C', u'3C', u'4C']
[u'1D', u'2D', u'3D', u'4D']


4. values are saved the list


__author__ = 'Administrator'
import xlrd
wb = xlrd.open_workbook("C:\\test.xlsx")
ws = wb.sheet_by_index(0)
ncol = ws.ncols
nlow = ws.nrows
print "-------- Sheet1 --------"
print "Number of col: " + str(ncol)
print "Number of low: " + str(nlow)
print "-------- Values of Excel file --------"
i = 0
j = 0
low = []
list = []
while i < nlow :
while j < ncol :
low.append(str(ws.row_values(i)[j]))
j += 1
list.append(low)
low = []
i += 1
j = 0
i = 0
while i < 4 :
print list[i]
i += 1
view raw gistfile1.txt hosted with ❤ by GitHub
row_values(0)이 [1A, 1B, 1C, 1D]의 정보를 가지고 있습니다.

결론적으로 1A만 불러오려면 row_values(0)[0]을 이용하면 1A만 출력 할 수 있습니다.

이를 통해 Python의 list에 엑셀파일의 모든 컬럼이 가지고있는 값을 저장 할 수 있습니다.

출력결과 아래와 같습니다.

-------- Sheet1 --------
Number of col: 4
Number of low: 4
-------- Values of Excel file --------
['1A', '1B', '1C', '1D']
['2A', '2B', '2C', '2D']
['3A', '3B', '3C', '3D']
['4A', '4B', '4C', '4D']

Related Posts: