excel文件读、写、修改 类和方法封装

import openpyxl
from openpyxl.styles import Border,Side,Font
import time

class parseExcel(object):
def __init__(self):
self.workbook=None
self.excelFile=None
self.font=Font(color=None)
self.RGBDict={"red":"FFFF3030","green":"FF008B00"}

def loadwokebook(self,excelPathAndName):
try:
# 将excel文件加入到内存,返回workbook对象,excelPathAndName传入文件路径
self.workbook=openpyxl.load_workbook(excelPathAndName)
except Exception,e:
raise e
self.excelFile=excelPathAndName #文件的路径
return self.workbook

def getSheetByName(self,sheetName):
#根据sheet名称获取sheet对象
try:
sheet=self.workbook.get_sheet_by_name(sheetName)
return sheet
except Exception,e:
raise e

def getSheetByIndex(self,sheetIndex):
#根据sheet索引获取sheet对象
try:
# sheetnames方法返回所有sheet名称到list,根据下标获取sheet名称
sheetname = self.workbook.sheetnames[sheetIndex]
except Exception, e:
raise e
sheet = self.workbook.get_sheet_by_name(sheetname) #根据sheet名称获取sheet对象
return sheet

def getRowsNumber(self,sheet):
#最大行
return sheet.max_row #根据sheet对象获取该sheet页最大行数

def getColsNumber(self, sheet):
# 最大列
return sheet.max_column

def getRow(self,sheet,RowNo):
# 获取某行对象,传入sheet对象,传RowNo要获取的行从0开始,0为第一行
try:
# sheet.rows获取所有行对象,存入list,RowNo下标获取指定行的对象
return list(sheet.rows)[RowNo]
except Exception,e:
raise e

def getColumn(self, sheet, ColumnNo):
# 获取某列对象,传入sheet对象,传ColumnNo要获取的列从0开始,0为第一列
try:
# sheet.rows获取所有列对象,存入list,ColumnNo下标获取指定列的对象
return list(sheet.columns)[ColumnNo]
except Exception, e:
raise e

def getCellOfValue(self,sheet,coordinate=None,RowNO=None,ColsNO=None):
'''读取单元个内容,两种方式
1.只传入coordinate 单元格参数("A2")
2.传入RowNO单元格的横坐标,ColsNO单元格的纵坐标
'''
if coordinate!=None: #如果coordinate参数不为空
try:
return sheet[coordinate].value #sheet对象传入单元格坐标,获取单元格的内容
except Exception,e:
raise e

elif coordinate is None and RowNO is not None and ColsNO is not None:#如果coordinate参数为空,RowNO和ColsNO不为空
try:
return sheet.cell(row=RowNO,column=ColsNO).value #传入行和列,坐标从1开始
except Exception,e:
raise e
else:
raise Exception("insufficient coordinates of cell")

def writeCell(self,sheet,content,coordinate=None, RowNO=None, ColsNO=None):
'''往已有excel表里写入数据,两种方式写入
1.只传入coordinate 单元格参数("A2")
2.传入RowNO单元格的横坐标,ColsNO单元格的纵坐标
'''
if coordinate is not None:#如果coordinate参数不为空
try:
sheet[coordinate].value = content #sheet对象传入单元格坐标,给单元格传入新值content
self.workbook.save(self.excelFile)
except Exception, e:
raise e
elif coordinate == None and RowNO is not None and ColsNO is not None:#如果coordinate参数为空,RowNO和ColsNO不为空
try:
sheet.cell(row=RowNO, column=ColsNO).value = content #传入行和列,坐标从1开始
self.workbook.save(self.excelFile) #对workbook进行保存,若没此句则不会修改成功
except Exception, e:
raise e
else:
raise Exception("insufficient coordinates of cell")

def writeCellCurrentTime(self, sheet, coordinate=None, RowNO=None, ColsNO=None):
# 往已有excel表里写入当前时间
now=int(time.time()) #显示当前时间戳
timeArray=time.localtime(now)
CurrentTime=time.strftime("%Y-%m-%d %H:%M:%S", timeArray)

if coordinate is not None:
try:
sheet[coordinate].value=CurrentTime
self.workbook.save(self.excelFile)
except Exception,e:
raise e
elif coordinate == None and RowNO is not None and ColsNO is not None:
try:
sheet.cell(row=RowNO,column=ColsNO).value=CurrentTime
self.workbook.save(self.excelFile)
except Exception,e:
raise e
else:
raise Exception("insufficient coordinates of cell")

if __name__=="__main__":
# 测试excel读取
dir = u"E:/pythontest/DataDrivenFrameWork/testDate/ceshi.xlsx"

pe = parseExcel()
pe.loadwokebook(dir)
sheet = pe.getSheetByIndex(0) # 返回sheet对象
# print pe.getRowsNumber(sheet) #获取最大行
# print pe.getColsNumber(sheet) #获取最大列
#
# 获取第一行所有对象,循环第一行所有对象并打印所有对象的值
a = pe.getRow(sheet, 0)
for i in a:
print i.value
# 获取第一列所有对象,循环第一列所有对象并打印所有对象的值
b = pe.getColumn(sheet, 0)
for i in b:
print i.value

# 获取单元格内容
print pe.getCellOfValue(sheet, "A1") # 传入坐标参数获取单元格内容
print pe.getCellOfValue(sheet, RowNO=1, ColsNO=1) # 传入行和列参数

# 测试单元格里写入数据
pe.writeCell(sheet, "测试测试", "A1") # 往A1单元格写入内容
pe.writeCell(sheet, "xxxxx", RowNO=1, ColsNO=1) # 传入行和列参数 行和列的错标从1开始

# 测试单元格里写入时间
pe.writeCellCurrentTime(sheet, "A1") # 往A1单元格写入内容
pe.writeCellCurrentTime(sheet, RowNO=1, ColsNO=1) # 传入行和列参数 行和列的错标从1开始

 

posted @ 2018-06-13 17:55  大米粒粒  阅读(537)  评论(0编辑  收藏  举报