#!/usr/bin/env python
# -*- coding:UTF-8 -*-
import requests
from bs4 import BeautifulSoup
from bs4 import NavigableString
import os
import requests
import xlrd
class ExcelUtil():
def __init__(self, excelPath, sheetName):
self.data = xlrd.open_workbook(excelPath)
self.table = self.data.sheet_by_name(sheetName)
# 获取第一行作为 key 值
self.keys = self.table.row_values(0)
# 获取总行数
self.rowNum = self.table.nrows
# 获取总列数
self.colNum = self.table.ncols
# print(self.keys[0],self.keys[1])
def dict_data(self):
if self.rowNum <=1:
print("总行数小于1")
else:
result=[]
for row in range(self.rowNum-1):
cols={}
rows_value=self.table.row_values(row)
for col in range(self.colNum):
cols[self.keys[col]]=rows_value[col]
result.append(cols)
return result
if __name__ == "__main__":
filepath = "E:\\常用文件\\测试资料\\账号.xlsx"
sheetName = "Sheet1"
data = ExcelUtil(filepath, sheetName)
print(data.dict_data())