import xlrd import xlwt path = input("请输入文件路径") listnum = ["0","1","2","3","4","5","6","7","8","9"] listSign = [] workbook = xlrd.open_workbook(path) sheet = workbook.sheet_by_index(0)
#sheet = workbook.sheet_by_name(sheetname) outfile = xlwt.Workbook(encoding='utf-8') sheetout = outfile.add_sheet('sheet1') for row in range(0,sheet.nrows): for i in range(0,len(sheet.row(row)[0].value)): if(((sheet.row(row)[0].value[i] in listnum or sheet.row(row)[0].value[i]=='.')and not(sheet.row(row)[0].value[i-1] in listnum or sheet.row(row)[0].value[i-1]=='.'))or(not(sheet.row(row)[0].value[i] in listnum or sheet.row(row)[0].value[i]=='.')and(sheet.row(row)[0].value[i-1] in listnum or sheet.row(row)[0].value[i-1]=='.'))): listSign.append(i) print(listSign) sheetout.write(row,0,sheet.row(row)[0].value[0:listSign[0]]) for i in range(0,len(listSign)-1): sheetout.write(row,i+1,sheet.row(row)[0].value[listSign[i]:listSign[i+1]]) sheetout.write(row,len(listSign),sheet.row(row)[0].value[listSign[len(listSign)-1]:]) listSign = [] outfile.save('outputfile1.xls')
把三列拆开右边到左边。
import xlrd import xlwt path = input("请输入文件路径") workbook = xlrd.open_workbook(path) sheet = workbook.sheet_by_index(0) ##for row in range(2,sheet.nrows): ## print() ## for col in range(sheet.ncols): ## print("%7s"%sheet.row(row)[col].value,' ',end='') # print(int(sheet.row(row)[col].value)) #sheet.nrows #pathout = input("请输入输出文件路径") outfile = xlwt.Workbook(encoding='utf-8') sheetout = outfile.add_sheet('sheet1') sheetout.write(0,0,'房间号') sheetout.write(0,1,'电表名称') sheetout.write(0,2,'电表号') j=0 for i in range(0,sheet.ncols,2): for row in range(2,sheet.nrows): if type(sheet.row(row)[i+1].value).__name__=='float': j=j+1 sheetout.write(j,0,sheet.row(row)[i].value) num=sheet.row(row)[i+1].value+180613000000 num=int(num) sheetout.write(j,1,num) sheetout.write(j,2,num) outfile.save('outputfile.xls')