【转】http://www.cnblogs.com/belid/archive/2013/05/02/3054119.html

一 对excel读操作

  安装xlrd模块,去官网http://pypi.python.org/pypi/xlrd下载,pyhon setup.py build ,python setup.py install。下面一个demo有详细的使用。

一行一列    一行二列    一行三列
二行一列    二行二列    二行三列
4    5    6
7    8    9
execl(test.xlsx)文件的内容
复制代码
 1 #coding: utf8
 2 import xlrd
 3 
 4 execl_name=r"F:\python_source\test.xlsx"
 5 print "打开excel文件读取数据".decode('utf8').encode('gbk') #注意编码转换
 6 data=xlrd.open_workbook(execl_name)
 7 
 8 table=data.sheets()[0]                #通过索引顺序获取
 9 #table=data.sheet_by_index(0)        #通过索引顺序获取
10 #table=data.sheet_by_name('Sheet1')  #通过名称获取
11 print "-----------------------------------"
12 print table.row_values(2)         #获取第三行整行的值(数组)
13 print table.col_values(2)          #获取第三列整列的值(数组)
14 print "-----------------------------------"
15 
16 print "打印第一行所有的值:".decode('utf8')
17 for i in table.row_values(0):
18     print i,                           #打印第一行所有的值
19 print 
20 
21 print '-----------------------------------'
22 print "行数".decode('utf8')+":",table.nrows                 #行数
23 print "列数".decode('utf8')+":",table.ncols                 #列数
24 print "-----------------------------------"
25 
26 print "输出execl所有值:".decode('utf8')
27 for x in range(table.nrows):
28     for i in table.row_values(x):
29         print i,
30     #print table.row_values(x)
31 print
32 
33 print '-----------------------------------'
34 print "单元格:".decode('utf8')
35 print table.cell(0,0).value         #单元格,一行一列
36 print table.cell(3,2).value            #四行三列
37 print '-----------------------------------'
38 
39 #cell_A1=table.row(0)[0].value 
40 print "cell_A1 %s" % table.row(0)[0].value  #一行一列
41 print "cell_B1 %s" % table.row(0)[1].value  #一行二列
42 print '----------------------------------'
43 
44 for i in range(table.ncols):
45     print table.row(1)[i].value             #输出所有第二行的值
46 print '----------------------------------'
47 
48 for i in range(table.nrows):
49     print table.col(1)[i].value             #输出所有第二列的值
复制代码

输出结果:

复制代码
打开excel文件读取数据
-----------------------------------
[4.0, 5.0, 6.0]
[u'\u4e00\u884c\u4e09\u5217', u'\u4e8c\u884c\u4e09\u5217', 6.0, 9.0]
-----------------------------------
打印第一行所有的值:
一行一列 一行二列 一行三列
-----------------------------------
行数: 4
列数: 3
-----------------------------------
输出execl所有值:
一行一列 一行二列 一行三列 二行一列 二行二列 二行三列 4.0 5.0 6.0 7.0 8.0 9.0
-----------------------------------
单元格:
一行一列
9.0
-----------------------------------
cell_A1 一行一列
cell_B1 一行二列
----------------------------------
二行一列
二行二列
二行三列
----------------------------------
一行二列
二行二列
5.0
8.0
复制代码

tips:由于给这个脚本文件取名为xlrd.py,然后在调试的时候一直没成功,是模块导入有问题,文件名和模块名重复了。

  二、对excel写操作

安装 pip install xlwt

复制代码
#coding:utf-8
import xlwt
import re 

wbk=xlwt.Workbook(encoding='cp936')  //修改编码
sheet=wbk.add_sheet('sheet 1')

'''
sheet.write(0,1,"test")   //写
sheet.write(0,2,"hello")
wbk.save('test.xls')    //保存
'''

sheet.write(0,0,"账号名".decode('utf8'))
sheet.write(0,1,"账号ID".decode('utf8'))
sheet.write(0,2,"角色名".decode('utf8'))
sheet.write(0,3,"等级".decode('utf8'))
sheet.write(0,4,"充值金额".decode('utf8'))
sheet.write(0,5,"注册时间".decode('utf8'))
sheet.write(0,6,"最后离线时间".decode('utf8'))
sheet.write(0,7,"注册IP".decode('utf8'))

a=[]
l1 = 1

fopen=open(r"E:\test\4.txt",'r')
for line in fopen:
    a=re.split(r'\s',line)
    #a=line.split(' ')
    sheet.write(l1,0,a[0])
    sheet.write(l1,1,a[1])
    sheet.write(l1,2,a[2])
    sheet.write(l1,3,a[3])
    sheet.write(l1,4,a[4])
    sheet.write(l1,5,a[5]+" "+a[6])
    sheet.write(l1,6,a[7])
    sheet.write(l1,7,a[9])
    l1 += 1
fopen.close()
wbk.save('6_7.xls')
复制代码