异想天开

What's the true meaning of light, Could you tell me why

python操作excel

日期:2015-05-22 15:27:15
  
最后更新日期:2015-05-22 15:28:36
读excel需要安装xlrd包,写excel需要安装xlwt包。
写excel示例:
[code lang="cpp"]
import xlwt
//可选,设置字符style
style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on',num_format_str='#,##0.00')
style1 = xlwt.easyxf(num_format_str='D-MMM-YY')
//打开excel
wb = xlwt.Workbook(encoding='utf8')
//增加worksheet
ws = wb.add_sheet('所有')
//写字段
ws.write(0,0,'类型')
ws.write(0,1,'导演')
ws.write(0,2,'主演')
//保存
wb.save('TOP250电影.xls')
[/code]

读excel示例:
[code lang="cpp"]
import xlrd
//打开excel
data = xlrd.open_workbook(filepath)
if (data):
table = data.sheets()[0]
row_total=table.nrows
col_index=find_index(table)
if (len(col_index))<1:
print "没有找到对应的列名"
exit(1)
for i in range(1,row_total):
serialid=table.cell(i,col_index['serialidindex']).value.strip()
title=table.cell(i,col_index['title']).value.strip()
title=title.encode("utf8")
else:
print "wrong"
[/code]
注意点:
1.excel里面字符串前面需要去掉空格
2.excel里面的字符是unicode,需要转换为utf8