首页 > 编程语言 >excel文件读写操作python

excel文件读写操作python

时间:2022-12-18 23:32:53浏览次数:41  
标签:sheet 读写操作 python self excel sheetName int rb def

#-*- coding: utf8 -*-
from os.pathimport join
from xlrdimport open_workbook
from xlutils.copyimport copy
import os,sys

class ReadAndWriteExcel:

def __init__(self):
self.FilePath=None
self.sheetName=None
self.rb=None

def Set_ExcelPath(self,FilePath):
self.FilePath=FilePath
self.rb=open_workbook(self.FilePath,'r+b')

#写数据
def Write_Excel(self,sheetName,rowIndex,lineIndex,content):
rbook=open_workbook(self.FilePath,'w')
wb=copy(rbook)
sheetIndex=rbook.sheet_names().index(sheetName)
wb.get_sheet(int(sheetIndex)).write(int(rowIndex),int(lineIndex),content)
wb.save(self.FilePath)
print 'write file ok'

# 获取某一行数据
def Get_RowIndexData_By_SheetName(self,sheetName,rowIndex):
return self.rb.sheet_by_name(sheetName).row_values(int(rowIndex))

#获取某一列数据
def Get_ColIndexData_By_SheetName(self,sheetName,colIndex):
return self.rb.sheet_by_name(sheetName).col_values(int(colIndex))

#获取总行数
def Get_RowCount_By_SheetName(self,sheetName):
return int(self.rb.sheet_by_name(sheetName).nrows)

# 获取总列数
def Get_columnCount_By_SheetName(self,sheetName):
return int(self.rb.sheet_by_name(sheetName).ncols)

# 获取某个单元格的值
def Get_Cell_By_ColIndex_RowIndex(self,sheetName,rowIndex,colIndex):
return self.rb.sheet_by_name(sheetName).cell_value(int(rowIndex),int(colIndex))



标签:sheet,读写操作,python,self,excel,sheetName,int,rb,def
From: https://blog.51cto.com/u_14181351/5951241

相关文章