天天看點

python導入excel子產品_python子產品xlrd讀取excel檔案

xlrd子產品簡介

python的xlrd子產品主要用以讀取和修改Excle檔案。

下載下傳位址:https://pypi.python.org/pypi。

xlrd子產品常用方法

Python

data = xlrd.open_workbook('excelFile.xls') #打開Excel檔案讀取資料

table = data.sheets()[0] #擷取一個工作表,通過索引順序擷取

table = data.sheet_by_index(0) #通過索引順序擷取

table = data.sheet_by_name(u'Sheet1')#通過名稱擷取

#擷取整行和整列的值(數組)

table.row_values(i)

table.col_values(i)

#擷取行數和列數

nrows = table.nrows

ncols = table.ncols

#循環行清單資料

for i in range(nrows ):

print table.row_values(i)

#單元格

cell_A1 = table.cell(0,0).value

cell_C4 = table.cell(2,3).value

#使用行列索引

cell_A1 = table.row(0)[0].value

cell_A2 = table.col(1)[0].value

#簡單的寫入

row = 0

col = 0

# 類型 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error

ctype = 1 value = '單元格的值'

xf = 0 # 擴充的格式化

table.put_cell(row, col, ctype, value, xf)

table.cell(0,0) #單元格的值'

table.cell(0,0).value #單元格的值'

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

data=xlrd.open_workbook('excelFile.xls')#打開Excel檔案讀取資料

table=data.sheets()[0]#擷取一個工作表,通過索引順序擷取

table=data.sheet_by_index(0)#通過索引順序擷取

table=data.sheet_by_name(u'Sheet1')#通過名稱擷取

#擷取整行和整列的值(數組)

table.row_values(i)

table.col_values(i)

#擷取行數和列數

nrows=table.nrows

ncols=table.ncols

#循環行清單資料

foriinrange(nrows):

printtable.row_values(i)

#單元格

cell_A1=table.cell(0,0).value

cell_C4=table.cell(2,3).value

#使用行列索引

cell_A1=table.row(0)[0].value

cell_A2=table.col(1)[0].value

#簡單的寫入

row=0

col=0

# 類型 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error

ctype=1value='單元格的值'

xf=0# 擴充的格式化

table.put_cell(row,col,ctype,value,xf)

table.cell(0,0)#單元格的值'

table.cell(0,0).value#單元格的值'

xlrd子產品使用執行個體

excel檔案

python導入excel子產品_python子產品xlrd讀取excel檔案

編寫代碼

Python

## Filename : lean001.py

# author by : www.py40.com

python操作Excel文檔

import xlrd

import xlwt

def read_file(Filename):

print(Filename)

# 打開檔案

workbook = xlrd.open_workbook(Filename)

# 擷取所有sheet

print(workbook.sheet_names())

# 根據sheet索引或者名稱擷取sheet内容

sheet1 = workbook.sheet_by_index(0) # sheet索引從0開始

# 也可以通過名字擷取索引 sheet1 = workbook.sheet_by_name('sheet1')

print(sheet1.name,sheet1.nrows,sheet1.ncols)

# 擷取整行和整列的值(數組)

rows = sheet1.row_values(1)

# 擷取第二行内容

cols = sheet1.col_values(2)

# 擷取第三列内容

print (rows)

print (cols)

# 擷取單元格内容

print (sheet1.cell(1,0))

print (sheet1.cell(1,0).value)

print (sheet1.row(1)[0].value)

if __name__ == '__main__':

read_file(r'E:\python\learn\demo.xls')

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

## Filename : lean001.py

# author by : www.py40.com

python操作Excel文檔

importxlrd

importxlwt

defread_file(Filename):

print(Filename)

# 打開檔案

workbook=xlrd.open_workbook(Filename)

# 擷取所有sheet

print(workbook.sheet_names())

# 根據sheet索引或者名稱擷取sheet内容

sheet1=workbook.sheet_by_index(0)# sheet索引從0開始

# 也可以通過名字擷取索引 sheet1 = workbook.sheet_by_name('sheet1')

print(sheet1.name,sheet1.nrows,sheet1.ncols)

# 擷取整行和整列的值(數組)

rows=sheet1.row_values(1)

# 擷取第二行内容

cols=sheet1.col_values(2)

# 擷取第三列内容

print(rows)

print(cols)

# 擷取單元格内容

print(sheet1.cell(1,0))

print(sheet1.cell(1,0).value)

print(sheet1.row(1)[0].value)

if__name__=='__main__':

read_file(r'E:\python\learn\demo.xls')

運作結果

Python

E:\python\python_tools.git\trunk\test>hello.py

E:\python\learn\demo1.xls

['sheet1']

sheet1 3 4

['1', '張三', '男', '1990-01-04']

['性别', '男', '女']

text:'1'

1

1

E:\python\python_tools.git\trunk\test>

1

2

3

4

5

6

7

8

9

10

11

E:\python\python_tools.git\trunk\test>hello.py

E:\python\learn\demo1.xls

['sheet1']

sheet134

['1','張三','男','1990-01-04']

['性别','男','女']

text:'1'

1

1

E:\python\python_tools.git\trunk\test>

簡單寫入Excel内容

Python

row = 0

col = 0

# 類型 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error

ctype = 1 value = '單元格的值'

xf = 0 # 擴充的格式化

table.put_cell(row, col, ctype, value, xf)

table.cell(0,0) #單元格的值'

table.cell(0,0).value #單元格的值'

1

2

3

4

5

6

7

8

row=0

col=0

# 類型 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error

ctype=1value='單元格的值'

xf=0# 擴充的格式化

table.put_cell(row,col,ctype,value,xf)

table.cell(0,0)#單元格的值'

table.cell(0,0).value#單元格的值'

日期資料的格式處理

我們再處理上面的檔案過程中發現,日期顯示為浮點數了。這個怎麼處理呢?

python讀取excel中單元格的内容傳回的有5種類型,即上面例子中的ctype:

Python

ctype : 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error

1

ctype:0empty,1string,2number,3date,4boolean,5error

即date的ctype=3,這時需要使用xlrd的xldate_as_tuple來處理為date格式,先判斷表格的ctype=3時xldate才能開始操作。

Python

if (sheet.cell(row,col).ctype == 3):

date_value = xlrd.xldate_as_tuple(sheet.cell_value(rows,3),book.datemode)

date_tmp = date(*date_value[:3]).strftime('%Y/%m/%d')

1

2

3

if(sheet.cell(row,col).ctype==3):

date_value=xlrd.xldate_as_tuple(sheet.cell_value(rows,3),book.datemode)

date_tmp=date(*date_value[:3]).strftime('%Y/%m/%d')

合并單元格的處理

讀取檔案的時候需要将formatting_info參數設定為True,預設是False,是以上面擷取合并的單元格數組為空,

Python

>>> book = xlrd.open_workbook(r'E:\python\learn\demo.xls',formatting_info=True,e

ncoding_override="utf-8")

>>> sheet1 = book.sheet_by_index(0)

>>> sheet1.merged_cells

[(6, 7, 2, 4), (1, 3, 3, 4)]

1

2

3

4

5

>>>book=xlrd.open_workbook(r'E:\python\learn\demo.xls',formatting_info=True,e

ncoding_override="utf-8")

>>>sheet1=book.sheet_by_index(0)

>>>sheet1.merged_cells

[(6,7,2,4),(1,3,3,4)]

merged_cells傳回的這四個參數的含義是:(row,row_range,col,col_range),其中[row,row_range)包括row,不包括row_range,col也是一樣。

(6, 7, 2, 4)的含義是:第6到7行(不包括6),2到4列(不包括2)合并。

(1, 3, 3, 4)的含義是:第1到3行(不包括1)3到4列(不包括3)合并。

利用這個,可以分别擷取合并的兩個單元格的内容:

Python

>>> print(sheet1.cell_value(6,2))

暫無資料

>>> print(sheet1.cell_value(1,3))

33242.0

1

2

3

4

>>>print(sheet1.cell_value(6,2))

暫無資料

>>>print(sheet1.cell_value(1,3))

33242.0