import xlrd
import xlwt
from xlutils import copy
# 写 excel
book=xlwt.Workbook()
sheet=book.add_sheet("sheet1")
sheet.write(0,0,"id")
sheet.write(0,1,"name")
sheet.write(1,0,"1")
sheet.write(1,1,"huahua")
book.save("students.xlsx") #微软 结尾用xlsx打不开; wps都可以
# 读 excel
book=xlrd.open_workbook("students.xlsx")
sheet=book.sheet_by_index(0)
# sheet=book.sheet_by_name("sheet1")
print(sheet.cell(0,0).value) #指定单元格
print(sheet.row_values(0)) #获取某一行的数据
print(sheet.col_values(0)) #获取某一列的数据
print(sheet.nrows) #总共有多少行
print(sheet.ncols) #总共有多少列
# 修改excel
from xlutils import copy
book=xlrd.open_workbook("students.xlsx")
new_book=copy.copy(book)
sheet=new_book.get_sheet(0) #这个是copy出来的,不是book对象了。
sheet.write(0,0,"编号")
new_book.save("students.xlsx")
python基础知识-操作excel
最新推荐文章于 2023-12-11 08:56:44 发布