get , write Excel

class readXls():

def read(self, name, sheetname):
try:
print('start read excel !')
self.path = os.path.abspath(os.path.join(os.path.dirname(__file__), name))
table = xlrd.open_workbook(self.path).sheet_by_name(sheetname)
xldata = []
for i in tqdm(range(1, table.nrows)):
xldata.append(table.row_values(i))
return xldata
except Exception as e:
raise e


def getsql(excelname, sheetname, filename):
if os.path.exists(filename):
os.system('type nul>{}'.format(filename))
sql_path = os.path.abspath(os.path.join(os.path.dirname(__file__), filename))
xls = readXls()
data = xls.read(excelname, sheetname)
print(' start write sql !')
with open(sql_path, 'a', encoding='utf-8') as f:
for i in tqdm(data):
postcode, teamcode, countrycode, = i
if isinstance(teamcode, str):
==========================================================================
class Write_excel(object):
def __init__(self, filename, sheetname='用例+报告'):

self.filename = filename
self.sheetname = sheetname

def write(self, i, j, value):
if not os.path.exists(self.filename):
wb = Workbook()
sh = wb.create_sheet(self.sheetname)
else:
wb = load_workbook(self.filename)
sh = wb[self.sheetname]
sh.cell(i, j).value = value
wb.save(self.filename)
原文地址:https://www.cnblogs.com/yaohu/p/12597126.html