python读excel文件,处理后,写入excel文件

1110阅读 0评论2016-10-14 u0402
分类:Python/Ruby

#/usr/bin/python 
#-*- coding: utf-8 -*-


import xlrd
import xlwt
import math
import string


data = xlrd.open_workbook('/root/work/test/shell/key_words.xls')


table = data.sheets()[0]


nrows = table.nrows
ncols = table.ncols

w = xlwt.Workbook(encoding='utf-8')
ws = w.add_sheet('result')


style = xlwt.XFStyle()


#写入第一行和第一列
for i in range(ncols):
ni = table.cell(0, i).value
nj = table.cell(i, 0).value
print ni
if (i != 0):
ws.write(0, i, nj, style)
ws.write(i, 0, ni, style)


#计算并写入
for i in range(nrows):
for j in range(ncols):
if (i != 0) and (j != 0):
#计算
ni = string.atof(table.cell(i,i).value)
nj = string.atof(table.cell(j,j).value)
nij = string.atof(table.cell(i,j).value)
s1 = nij / math.sqrt(ni * nj)
#写入计算结果到新的xls文件
ws.write(i, j, s1)
print s1,
print ' '

#保存写入excel的内容
w.save('result.xls')


上一篇:socket 大端 小端 转换
下一篇:linux内核list.h头文件分析(一)