# coding=gbk
__author__ = 'wds'
import xlrd
import xlwt
import sys
def pyExcel(infile = 'infile.xlsx', curday='2015-01-01'):
outfile = curday + '.xls'
key_walk = unicode("步", "gbk")
key_run = unicode("跑", "gbk")
key_more = unicode("多", "gbk")
key_less = unicode("少", "gbk")
key_fast = unicode("快", "gbk")
key_slow = unicode("慢", "gbk")
key_cha = unicode("差", "gbk")
key_buzhun = unicode("不准", "gbk")
key_record = unicode("记录", "gbk")
key_other = unicode("其他", "gbk")
key_duoshao = unicode("多少", "gbk")
key_tongbu = unicode("同步", "gbk")
key_class = unicode("类别", "gbk")
quan0 = unicode("全0问题", "gbk")
quan255 = unicode("255问题", "gbk")
try:
rbook = xlrd.open_workbook(infile)
except Exception,e:
print str(e)
rtable = rbook.sheet_by_index(0)
nrows = rtable.nrows
ncols = rtable.ncols
wbook = xlwt.Workbook(encoding='utf-8')
wtable = wbook.add_sheet(unicode("计步相关所有问题", "gbk"), cell_overwrite_ok=True)
wtable_buzhun = wbook.add_sheet(unicode("计步不准问题", "gbk"), cell_overwrite_ok=True)
wtable_others = wbook.add_sheet(unicode("其他相关问题", "gbk"), cell_overwrite_ok=True)
wtable.col(1).width = 20000
wtable.col(2).width = 5000
wtable.col(3).width = 5000
wtable.col(6).width = 5000
wtable.col(7).width = 8000
wtable_buzhun.col(1).width = 20000
wtable_buzhun.col(2).width = 5000
wtable_buzhun.col(3).width = 5000
wtable_buzhun.col(6).width = 5000
wtable_buzhun.col(7).width = 8000
wtable_others.col(1).width = 20000
wtable_others.col(2).width = 5000
wtable_others.col(3).width = 5000
wtable_others.col(6).width = 5000
wtable_others.col(7).width = 8000
style0 = xlwt.easyxf('font: height 240, bold True')
style1 = xlwt.easyxf('font: height 220; align: horizontal left; align: wrap on')
style2 = xlwt.easyxf('font: height 220, colour red; align: horizontal left; align: wrap on')
num = 0
num_buzhun = 0
num_other = 0
for i in range(0, nrows):
rowdata = rtable.row_values(i)
feedbackData = rowdata[1]
feedbackDataReplaced = feedbackData.replace(key_tongbu, '').replace(key_duoshao, '')
if (i<2 or feedbackDataReplaced.find(key_walk) >= 0 or feedbackDataReplaced.find(key_run) >= 0):
if i<2:
for j,coldata in enumerate(rowdata):
wtable.write(num, j, coldata, style0)
wtable_buzhun.write(num_buzhun, j, coldata, style0)
wtable_others.write(num_other, j, coldata, style0)
if i == 1:
wtable_buzhun.write(num_buzhun, ncols, key_class, style0)
num_buzhun += 1
num_other += 1
elif feedbackDataReplaced.find(key_more) >= 0 or feedbackDataReplaced.find(key_less) >= 0 \
or feedbackDataReplaced.find(key_cha) >= 0 or feedbackDataReplaced.find(key_buzhun) >= 0 \
or feedbackDataReplaced.find(key_fast) >= 0 or feedbackDataReplaced.find(key_slow) >= 0:
for j,coldata in enumerate(rowdata):
wtable.write(num, j, coldata, style2)
wtable_buzhun.write(num_buzhun, j, coldata, style1)
if feedbackDataReplaced.find(key_less) >= 0 or feedbackDataReplaced.find(key_cha) >= 0:
wtable_buzhun.write(num_buzhun, ncols, key_less, style2)
elif feedbackDataReplaced.find(key_buzhun) >= 0:
wtable_buzhun.write(num_buzhun, ncols, key_buzhun, style2)
elif feedbackDataReplaced.find(key_record) >= 0:
wtable_buzhun.write(num_buzhun, ncols, key_record)
else:
wtable_buzhun.write(num_buzhun, ncols, key_other)
num_buzhun += 1
else:
for j,coldata in enumerate(rowdata):
wtable.write(num, j, coldata, style1)
wtable_others.write(num_other, j, coldata, style1)
num_other += 1
num += 1
try:
wbook.save(outfile)
except Exception,e:
print str(e)
print 'rows: ', nrows
print 'cols: ', ncols
print 'Found %d about steps' %(num-2)
if __name__=='__main__':
#pyExcel()
pyExcel(unicode(sys.argv[1], "gbk"), sys.argv[2])
python 读写Excel
最新推荐文章于 2024-07-21 22:27:18 发布