#读excel
Import xlrd
Book = Xlrd.open_workbook (R ' students.xlsx ')
#打开excel
Print (Book.sheet_names ())
#获取所有sheet的名字
Sheet = book.sheet_by_index (0)
#根据sheet页的位置去取sheet
Sheet2 = 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 (). Value)
#cell方法是获取指定单元格的数据, preceded by rows, followed by columns
#读excel的时候, XLS xlsx can be read
#写excel
Import XLWT
Book = XLWT. Workbook ()
#新建一个excel对象
Sheet = book.add_sheet (' stu ')
#添加一个sheet页
Sheet.write (0,0, ' number ') #行的下标, column subscript, fill in the contents
Book.save (' Stu.xls ')
#写excel的时候, only XLS can be manipulated
#修改Excel, a new copy of the Excel object is required
From xlutils.copy Import copy
Book = Xlrd.open_workbook (' New_stu.xls ')
#打开原来的excel
New_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 ')
Python operations Excel