# coding=utf-8
"""
@Time : 2020/2/29 15:32
@Author : hzsyy
@FileName : getExcelData.py
@IDE : PyCharm
"""
import os
from openpyxl import *
from data import datas
data = datas()
root_dir = os.path.dirname(os.path.abspath(__file__))
class get_excel_data():
def __init__(self, file):
self.file = file
self.wb = load_workbook(self.file)
sheets = self.wb.get_sheet_names()
self.sheet = sheets[0]
self.ws = self.wb[self.sheet]
# 获取表格的总行数和总列数
def getRowsClosNum(self):
rows = self.ws.max_row
columns = self.ws.max_column
return rows, columns
# 获取某个单元格的值
def getCellValue(self, row, column):
cellvalue = self.ws.cell(row=row, column=column).value
return cellvalue
# 获取某列的所有值
def getColValues(self, column):
rows = self.ws.max_row
columndata = []
for i in range(1, rows + 1):
cellvalue = self.ws.cell(row=i, column=column).value
columndata.append(cellvalue)
return columndata
# 获取某行所有值
def getRowValues(self, row):
columns = self.ws.max_column
rowdata = []
for i in range(1, columns + 1):
cellvalue = self.ws.cell(row=row, column=i).value
rowdata.append(cellvalue)
return rowdata
def writeExcel(self, number):
wb = Workbook()
ws = wb.create_sheet('sheet1', 0)
ws.cell(row=1, column=1, value="定义规则")
ws.cell(row=2, column=2, value="1,遵循手机号码规则")
ws.cell(row=3, column=2, value="2,手机号码不要重复")
ws.cell(row=4, column=1, value="序号")
ws.cell(row=4, column=2, value="手机号码")
for rows in range(number):
phone = data.getData()["phone_number"]
ws.cell(row=rows+5, column=1, value=rows+1)
ws.cell(row=rows+5, column=2, value=phone)
print("写入第{}条手机号>>>>>>>>>>>{}".format(rows+1, phone))
wb.save(self.file)
if __name__ == "__main__":
excelPath = os.path.join(root_dir, "测试数据.xlsx")
excelInfo = get_excel_data(excelPath)
excelInfo.writeExcel(5000)
# rows = excelInfo.getRowsClosNum()
# for i in range(2, rows[0]+1):
# carInfo = excelInfo.getRowValues(i)
# print(carInfo)
# carNo = carInfo[1]
# carModel = carInfo[2]
# carColour = carInfo[3]
# carSeatNumber = carInfo[4]
# carVin = carInfo[17]