python:excel操作
excel 文件操作写入数据
import xlwt book = xlwt.Workbook() sheet = book.add_sheet("sheet1") # sheet.write(0,0,"id") # sheet.write(1,0,"1") # book.save("test.xls") #结尾用xlsx的话,用office打开有问题,wps打开没问题 title = ["id","name","sex","city"] students = [ [1,"小黑","男","北京"], [2,"小白","男","北京"], [3,"candy","女","深圳"], [4,"小张","女","北京"], ] students.insert(0,title) # 插入文件头 # row = 0 #行号 # for student in students: #控制行 # col = 0 # 列号 # for value in student: #控制列 # sheet.write(row,col,value) # col+=1 # row+=1 for row,student in enumerate(students): for col,value in enumerate(student): sheet.write(row,col,value) book.save("test.xls")
excel操作读取数据
# import xlrd # book = xlrd.open_workbook("test.xls") # # sheet = book.sheet_by_name("sheet1") # sheet = book.sheet_by_index(0) # # book.sheets() #获取所有sheet页 # print(sheet.cell(0,0).value) #获取某一个单元格的内容 # print(sheet.row_values(0)) #获取整行的数据 # print(sheet.col_values(0)) #获取整列的数据 # print(sheet.nrows) #总共多少行 # print(sheet.ncols) #总共多少列