需求:把xls里所有sheet的内容以每行一条记录写入txt文件内!
import xlrd def read_xls(src_file,des_file): data = xlrd.open_workbook(src_file) file = open(des_file,'aw+') for sheet_num in xrange(len(data.sheets())): mysheet = data.sheets()[sheet_num] for row in xrange(0, mysheet.nrows): tmp = "" for col in xrange(0,mysheet.ncols): if mysheet.row_values(row): if mysheet.cell(row,col) not in ['playerid','power']: tmp += str(mysheet.cell(row,col).value)+" " #每行每格值列出来 file.write(tmp+' ') if __name__ == "__main__": read_xls('power.xls','result.txt')