import xlwt
import xlrd
def data_output(a,x_gap,y,y_gap,column1,column2,row1,row2,sheet1):
j=0
x=a-x_gap
b=a+x_gap/2
global index,lineNum
print("第%d组数据写入中..." % index)
while j<row1+row2:
if x==a-x_gap:
for i in range(column1):
x+=x_gap
#print("数据写入中:%d %.1f %.1f" %(index,x,y))
list_temp = [0,index,x,y]
lineNum += 1
for k in range(1,4):
sheet1.write(lineNum,k-1,list_temp[k])
x=b-x_gap
y+=y_gap
j+=1
else:
for i in range(column2):
x+=x_gap
#print("数据写入中:%d %.1f %.1f" %(index,x,y))
list_temp = [0,index,x,y]
lineNum += 1
for k in range(1,4):
sheet1.write(lineNum,k-1,list_temp[k])
x=a-x_gap
y+=y_gap
j+=1
index += 1
if __name__ == "__main__":
try:
#print("PS:输入的文件名需要加上对应的文件后缀,.xls或者.xlsx")
address_temp = input("请输入文件的地址:")
address = address_temp + ".xls"
file_name = input("文件名保存为:") + ".xls"
ExcelFile=xlrd.open_workbook(address)
except FileNotFoundError as e:
address = address_temp + ".xlsx"
ExcelFile=xlrd.open_workbook(address)
sheet=ExcelFile.sheet_by_index(0)
rowsNum = sheet.nrows
index = 1
lineNum = 0
f_wk = xlwt.Workbook() #创建工作簿
sheet1 = f_wk.add_sheet('sheet1',cell_overwrite_ok=True) #创建sheet
sheet1.write(0,0,"序号")
sheet1.write(0,1,"x")
sheet1.write(0,2,"y")
for num in range(1,rowsNum): # 数据从第二行开始,读取每一行的内容
rows=sheet.row_values(num)
a,x_gap,y,y_gap,column1,column2,row1,row2 = rows[1],rows[2],rows[3],rows[4],int(rows[5]),int(rows[6]),int(rows[7]),int(rows[8])
data_output(a,x_gap,y,y_gap,column1,column2,row1,row2,sheet1)
f_wk.save(file_name) #保存文件
print("数据保存成功,保存名字为【%s】" % file_name)
input("点击回车键退出。")