Import XLWT #写入excel
# book = XLWT. Workbook () #新建一个excel
# sheet = book.add_sheet (' Sheet1 ') #添加一个sheet页
# Sheet.write (0,0, ' name ')
# sheet.write (0,1, ' sex ')
# Sheet.write (0,2, ' age ')
# book.save (' Stu.xls ') #微软的office不能用xlsx结尾的, WPS casual
Stus = [
[' name ', ' age ', ' gender ', ' score '],
[' Mary ', 20, ' female ', 89.9],
[' Mary ', 20, ' female ', 89.9],
[' Mary ', 20, ' female ', 89.9],
[' Mary ', 20, ' female ', 89.9]
]
Book = XLWT. Workbook () #新建一个excel
sheet = book.add_sheet (' Sheet1 ') #添加一个sheet页
raw = 0# control line
For Stu in Stus:
cols = 0 #控制列
For s in Stu:
sheet.write (raw,cols,s)
cols+=1
raw+=1
book.save (' Kkk.xls ')
import xlrd# read Excel
book = Xlrd.open_workbook (' Stu.xls ') #打开一个excel
sheet = Book.sheet_by_index (0) #根据顺序获取sheet
# Sheet2 = book.sheet_by_name (' Sheet1 ') #根据sheet页名字获取sheet
# print (Sheet.cell (0,0). Value) #指定行和列获取数据
# print (sheet.ncols) #获取excel里面有多少列
# print (sheet.nrows) #获取excel里面有多少行
Sheet.row_values (1) #取第几行的数据
print (sheet.col_values (1)) #取第几列的数据
for I in Range (sheet.nrows): # 0 1 2 3 4 5
PR Int (sheet.row_values (i)) #取第几行的数据
From xlutils.copy import copy #修改excel
Import xlrd
Book1 = Xlrd.open_workbook (' Stu.xls ')
Book2 = Copy (Book1) #拷贝一份原来的excel
Sheet = book2.get_sheet (0) #获取第几个sheet页
Sheet.write (1,3,0)
Sheet.write (1, 0, ' Little black ')
Book2.save (' Stu.xls ')
Day7-python Learning Notes (16) Excel operations