python操作excel

it2022-05-05  144

#读excel import xlrdbook = xlrd.open_workbook(r'students.xlsx')#打开excelprint(book.sheet_names())#获取所有sheet的名字sheet = book.sheet_by_index(0)#根据sheet页的位置去取sheetsheet2 = book.sheet_by_name('Sheet2')#根据sheet页的名字获取sheet页print(sheet.nrows)#获取sheet页里面的所有行数print(sheet.ncols)#获取sheet页里面的所有列数print(sheet.row_values(0))#根据行号获取整行的数据print(sheet.col_values(0))#根据列获取整列的数据print(sheet.cell(1,1).value)#cell方法是获取指定单元格的数据,前面是行,后面是列#读excel的时候,xls xlsx都可以读#写excelimport xlwtbook = xlwt.Workbook()#新建一个excel对象sheet = book.add_sheet('stu')#添加一个sheet页sheet.write(0,0,'编号') #行的下标,列的下标,填写内容book.save('stu.xls')#写excel的时候,只能操作xls #修改Excel,需要新copy一个excel对象from xlutils.copy import copybook = xlrd.open_workbook('new_stu.xls')#打开原来的excelnew_book = copy(book)#通过xlutils里面copy复制一个excel对象sheet = new_book.get_sheet(0)#获取sheet页sheet.write(0,0,'id')new_book.save('new_stu_1.xls')

转载于:https://www.cnblogs.com/lazy-cat-home/p/7137144.html


最新回复(0)