python基礎 xlrd xlutils 讀excel和修改excel


#xlwt只能寫
import xlrd #只能讀
book=xlrd.open_workbook('user.xls')
sheet=book.sheet_by_index(0)#根據索引查找sheet
print(book.nsheets)
sheet2=book.sheet_by_name('學生信息')#根據sheet名稱查找sheet
print(sheet.cell(0,0).value)#第一行一列的單元格信息
print(sheet.cell(1,0).value)#第二行第一列的單元格信息
print(sheet.row_values(0))#第一行的信息,存到list里
print(sheet.nrows)#一共有多少行
print(sheet.col_values(0))#第一列的信息,存到list里
print(sheet.ncols)#一共多少列
print(sheet2.col_values(0))

修改excel
#修改excel
import xlrd
from xlutils import copy
book=xlrd.open_workbook('user.xls')#打開原來的excel
new_book=copy.copy(book)#copy一個新excel
sheet=new_book.get_sheet(0)#獲取第一個sheet頁
print(dir(sheet))#查看xlutils有什么方法
sheet.write(1,1,'ben')#修改第二行第二列的信息為ben
new_book.save('user.xls')#保存為和打開相同的excel名


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM