您的位置:首页 > 编程语言 > Python开发

python处理excel文档作为配置文件

2010-12-12 19:41 771 查看
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import xlrd
import sys
import time

def usage():
print "This Program need 4 parameters including command itself:i/n/tconfigfile is configfile format .xls/n/tparameter2 is test environment/n/tparameter3 is testcase/n/nFor Example :/n/tcommand configfile 0 3/n/tcommand configfile 1 5/n/tcommand configfile 0 5/n/nEditor and Programmer:/n/n"
sys.exit()

def column_num(i):
columntypes = sheet_0.col_types(i)
column_list_temp = []
#	print len(columntypes)
for j in range(len(columntypes)):
if columntypes[j] == 1 or columntypes[j] == 2:
column_list_temp.append(j)
column_list_temp.append(sheet_0.nrows)
return column_list_temp

def action_pro(str, i):
print 'Action %s Times %d is running ...'%(str, i)

def case_pro(case_str):
#	times_act = int(case_str.split(',')[1])
for i in range(int(case_str.split(',')[1])):
#		print case_str.split(',')[0]
try:
action_pro(case_str.split(',')[0],i)
except:
print 'Action %s Times %d run Failed ...'%(case_str.split(',')[0], i)
continue
time.sleep(1)
print 'Finish one step/n'

def testcase_pro(string_temp):
actions = string_temp.split(';')
for act in actions:
try:
case_pro(act)
except:
print 'Test Case Action %s run Failed ...'%(act.split(',')[0])
continue
print time.strftime('%Y-%m-%d-%H-%M-%S'),'/nFinish one case/n'

def test_bed(str_bed):
print 'Test Bed %s is preparing ... and Finished'%str_bed

def row_print(column_list_temp2, cols):
length_coln0 = len(column_list_temp2)
#	print '/n/n',length_coln0
for i in range(length_coln0 - 1):
#		print column_list_temp2[i]
print '/n/Start Server Configuration: '
try:
test_bed(sheet_0.cell_value(column_list_temp2[i], coln))
except:
print 'Test Bed %s prepare Failed ...'%sheet_0.cell_value(column_list_temp2[i], coln)
continue
#			sys.exit()
for k in range(column_list_temp2[i] + 1, column_list_temp2[i + 1]):
#			print sheet_0.cell_value(k, cols)
testcase_pro(sheet_0.cell_value(k, cols))

def main():
global coln
coln = int(sys.argv[2])
global column_list
column_list = column_num(coln)
#	print column_list
row_print(column_list, int(sys.argv[3]))

if __name__ == '__main__':
if len(sys.argv) != 4:
print usage()
filename = sys.argv[1]
try:
workbook = xlrd.open_workbook(filename,'r')
sheet_0 = workbook.sheet_by_name('Sheet1')
except:
print "Error Occur, and Stop ."
sys.exit()
main()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: