Perfect at 07011111.
This commit is contained in:
parent
8697f919e2
commit
536604b148
|
@ -28,10 +28,17 @@ class Excel:
|
|||
self.__readonly__ = read_only
|
||||
if self.__workbook__:
|
||||
raise Exception('文件已经打开 | File has been opened.')
|
||||
self.__filename__ = os.path.abspath(filename)
|
||||
self.__workbook__ = openpyxl.load_workbook(filename, read_only=read_only)
|
||||
self.select(sheet)
|
||||
self.__autosave__ = auto_save
|
||||
if filename:
|
||||
self.__filename__ = os.path.abspath(filename)
|
||||
self.__workbook__ = openpyxl.load_workbook(filename, read_only=read_only)
|
||||
self.__autosave__ = auto_save
|
||||
self.select(sheet)
|
||||
else:
|
||||
self.__workbook__ = openpyxl.Workbook()
|
||||
self.__readonly__ = False
|
||||
self.__autosave__ = False
|
||||
self.select(None)
|
||||
self.sheetRename('Sheet1')
|
||||
return self
|
||||
|
||||
def select(self, sheet):
|
||||
|
@ -75,6 +82,35 @@ class Excel:
|
|||
"""
|
||||
return self.__workshet__.max_column
|
||||
|
||||
def sheetRename(self, name=None):
|
||||
if not name:
|
||||
name = 'Sheet'
|
||||
self.__workshet__.title = name
|
||||
self.saveAuto()
|
||||
return True
|
||||
|
||||
def sheetCreate(self, name=None):
|
||||
if not name:
|
||||
currentList = self.sheetList()
|
||||
nextNo = len(currentList) + 1
|
||||
for i in range(0, 99):
|
||||
name = 'Sheet' + str(nextNo)
|
||||
if name in currentList:
|
||||
nextNo = nextNo + 1
|
||||
continue
|
||||
else:
|
||||
break
|
||||
self.__workbook__.create_sheet(name)
|
||||
self.select(name)
|
||||
self.saveAuto()
|
||||
return self
|
||||
|
||||
def sheetDelete(self):
|
||||
self.__workbook__.remove(self.__workshet__)
|
||||
self.select(0)
|
||||
self.saveAuto()
|
||||
return self
|
||||
|
||||
def cellSelect(self, cell):
|
||||
"""设置活动单元格 | Set active cell.
|
||||
:param cell:
|
||||
|
@ -110,60 +146,8 @@ class Excel:
|
|||
self.saveAuto()
|
||||
return True
|
||||
|
||||
def cellMultView(self, area='', from_col=0, from_row=0, to_col=0, to_row=0):
|
||||
minCol = from_col
|
||||
minRow = from_row
|
||||
maxCol = to_col
|
||||
maxRow = to_row
|
||||
if area:
|
||||
area = area + ':'
|
||||
area = area.upper()
|
||||
area = area.split(':')
|
||||
minCell = area[0]
|
||||
maxCell = area[1]
|
||||
minColList = re.findall('([A-Z]+)', minCell)
|
||||
minRowList = re.findall('([0-9]+)', minCell)
|
||||
maxColList = re.findall('([A-Z]+)', maxCell)
|
||||
maxRowList = re.findall('([0-9]+)', maxCell)
|
||||
minColString = (minColList and minColList[0]) or ''
|
||||
minRowString = (minRowList and minRowList[0]) or ''
|
||||
maxColString = (maxColList and maxColList[0]) or ''
|
||||
maxRowString = (maxRowList and maxRowList[0]) or ''
|
||||
ncols = self.sheetNcol()
|
||||
nrows = self.sheetNrow()
|
||||
if minCell:
|
||||
minCol = minColString or 'A'
|
||||
minCol = column_index_from_string(minCol) - 1
|
||||
# print(minCol)
|
||||
minRow = minRowString or '1'
|
||||
minRow = int(minRow) - 1
|
||||
# print(minRow)
|
||||
if maxCell:
|
||||
if minColString and minRowString:
|
||||
maxCol = maxColString or get_column_letter(ncols)
|
||||
maxCol = column_index_from_string(maxCol) - 1
|
||||
# print(maxCol)
|
||||
maxRow = maxRowString or nrows
|
||||
maxRow = int(maxRow) - 1
|
||||
# print(maxRow)
|
||||
elif minColString and minRowString == '':
|
||||
maxCol = column_index_from_string(maxColString) - 1
|
||||
maxRow = nrows - 1
|
||||
elif minRowString and minColString == '':
|
||||
maxCol = ncols - 1
|
||||
maxRow = int(maxRowString) - 1
|
||||
else:
|
||||
if minColString and minRowString:
|
||||
maxCol = minCol
|
||||
maxRow = minRow
|
||||
elif minColString and minRowString == '':
|
||||
maxCol = minCol
|
||||
maxRow = nrows - 1
|
||||
elif minRowString and minColString == '':
|
||||
maxCol = ncols - 1
|
||||
maxRow = minRow
|
||||
cellList = self.__workshet__._cells_by_row(min_col=minCol + 1, min_row=minRow + 1, max_col=maxCol + 1,
|
||||
max_row=maxRow + 1)
|
||||
def cellGetView(self, area='', from_col=0, from_row=0, to_col=0, to_row=0):
|
||||
cellList = self._viewGetOfCell(area=area, from_col=from_col, from_row=from_row, to_col=to_col, to_row=to_row)
|
||||
result = []
|
||||
for lineList in cellList:
|
||||
lineResult = []
|
||||
|
@ -172,6 +156,23 @@ class Excel:
|
|||
result.append(lineResult)
|
||||
return result
|
||||
|
||||
def cellPutView(self, area='', from_col=0, from_row=0, to_col=0, to_row=0, value=None):
|
||||
if not value or not isinstance(value, list):
|
||||
return False
|
||||
values = self._viewGetOfCell(area=area, from_col=from_col, from_row=from_row, to_col=to_col, to_row=to_row)
|
||||
valuesLen = len(values)
|
||||
for i in range(0, valuesLen):
|
||||
online = values[i]
|
||||
onlineLen = len(online)
|
||||
for j in range(0, onlineLen):
|
||||
oncell = online[j]
|
||||
try:
|
||||
oncell.value = value[i][j]
|
||||
except:
|
||||
pass
|
||||
self.saveAuto()
|
||||
return True
|
||||
|
||||
def save(self, filename=None):
|
||||
"""保存 | Save.
|
||||
:param filename: 另存为的文件路径,默认为保存文件 | The file path to save as, the default is to save the file.
|
||||
|
@ -201,41 +202,104 @@ class Excel:
|
|||
self.__workbook__ = None
|
||||
self.__filename__ = None
|
||||
self.__autosave__ = None
|
||||
self.__readonly__ = None
|
||||
return True
|
||||
|
||||
def _viewGetOfCell(self, area='', from_col=0, from_row=0, to_col=0, to_row=0):
|
||||
minCol = from_col
|
||||
minRow = from_row
|
||||
maxCol = to_col
|
||||
maxRow = to_row
|
||||
if area:
|
||||
area = area + ':'
|
||||
area = area.upper()
|
||||
area = area.split(':')
|
||||
minCell = area[0]
|
||||
maxCell = area[1]
|
||||
minColList = re.findall('([A-Z]+)', minCell)
|
||||
minRowList = re.findall('([0-9]+)', minCell)
|
||||
maxColList = re.findall('([A-Z]+)', maxCell)
|
||||
maxRowList = re.findall('([0-9]+)', maxCell)
|
||||
minColString = (minColList and minColList[0]) or ''
|
||||
minRowString = (minRowList and minRowList[0]) or ''
|
||||
maxColString = (maxColList and maxColList[0]) or ''
|
||||
maxRowString = (maxRowList and maxRowList[0]) or ''
|
||||
ncols = self.sheetNcol()
|
||||
nrows = self.sheetNrow()
|
||||
if minCell:
|
||||
minCol = minColString or 'A'
|
||||
minCol = column_index_from_string(minCol) - 1
|
||||
minRow = minRowString or '1'
|
||||
minRow = int(minRow) - 1
|
||||
if maxCell:
|
||||
if minColString and minRowString:
|
||||
maxCol = maxColString or get_column_letter(ncols)
|
||||
maxCol = column_index_from_string(maxCol) - 1
|
||||
maxRow = maxRowString or nrows
|
||||
maxRow = int(maxRow) - 1
|
||||
elif minColString and minRowString == '':
|
||||
maxCol = column_index_from_string(maxColString) - 1
|
||||
maxRow = nrows - 1
|
||||
elif minRowString and minColString == '':
|
||||
maxCol = ncols - 1
|
||||
maxRow = int(maxRowString) - 1
|
||||
else:
|
||||
if minColString and minRowString:
|
||||
maxCol = minCol
|
||||
maxRow = minRow
|
||||
elif minColString and minRowString == '':
|
||||
maxCol = minCol
|
||||
maxRow = nrows - 1
|
||||
elif minRowString and minColString == '':
|
||||
maxCol = ncols - 1
|
||||
maxRow = minRow
|
||||
cellList = self.__workshet__._cells_by_row(min_col=minCol + 1, min_row=minRow + 1,
|
||||
max_col=maxCol + 1, max_row=maxRow + 1)
|
||||
result = []
|
||||
for lineList in cellList:
|
||||
lineResult = []
|
||||
for cellObject in lineList:
|
||||
lineResult.append(cellObject)
|
||||
result.append(lineResult)
|
||||
return result
|
||||
|
||||
excel = Excel().open(filename='../../example.xlsx', read_only=False, auto_save=True)
|
||||
# print(excel.sheetList())
|
||||
|
||||
excel.select(0)
|
||||
# print(excel.cellMultView(area='H9:L11'))
|
||||
# print(excel.cellMultView(area='1:3'))
|
||||
# print(excel.cellMultView(area='A:C'))
|
||||
# print(excel.cellMultView(area='4'))
|
||||
# print(excel.cellMultView(area='D'))
|
||||
# print(excel.cellMultView(area='D1'))
|
||||
# print(excel.cellMultView(area='A1:F1', from_col=7, from_row=8, to_col=11, to_row=10))
|
||||
print(excel.cellMultView(area='F'))
|
||||
|
||||
for i in range(0, 7):
|
||||
excel.cellPut(row=9, col=7 + i, value=i * 10 + 1)
|
||||
|
||||
print(colIndexFromString('D'))
|
||||
# print(excel.sheetName())
|
||||
# print(excel.sheetNrow())
|
||||
# print(excel.sheetNcol())
|
||||
# print(excel.cellPut(row=0, col=0, value='id777'))
|
||||
# print(excel.cellGet(row=0, col=0))
|
||||
|
||||
# print(excel.save())
|
||||
# print(excel.exit())
|
||||
|
||||
# a = openpyxl.load_workbook('../../example.xlsx')
|
||||
# print(a.active._cells_by_row(min_row=1, min_col=1, max_row=3, max_col=3).__next__())
|
||||
|
||||
# # 根据列的数字返回字母
|
||||
# print(get_column_letter(8)) # B
|
||||
# # 根据字母返回列的数字
|
||||
# print(column_index_from_string('ab')) # 4
|
||||
#
|
||||
# print(re.findall('([A-Za-z]+)', 'AB2'))
|
||||
if __name__ == '__main__':
|
||||
# Example.
|
||||
# 创建新的Excel文件
|
||||
# excel = Excel().open()
|
||||
# 打开现有Excel文件
|
||||
# excel = Excel().open(filename='./example.xlsx')
|
||||
excel = Excel().open(filename='./example.xlsx', read_only=False, auto_save=False)
|
||||
print(excel.sheetList())
|
||||
excel.select(0)
|
||||
excel.select('Sheet1')
|
||||
print(excel.cellGet(cell='A1'))
|
||||
print(excel.cellPut(cell='A1', value='标题'))
|
||||
print(excel.cellGetView(area='A1:F5'))
|
||||
print(excel.cellGetView(area='A:C'))
|
||||
print(excel.cellGetView(area='1:5'))
|
||||
print(excel.cellGetView(area='A1'))
|
||||
print(excel.cellGetView(area='A'))
|
||||
print(excel.cellGetView(area='1'))
|
||||
print(excel.cellPutView(area='A1:Z256', value=
|
||||
[
|
||||
['1', '2', '3', '4', '5'],
|
||||
['A', 'B', 'C', 'D', 'E'],
|
||||
['A', 'B', 'C', 'D', 'E'],
|
||||
['A', 'B', 'C', 'D', 'E']
|
||||
]
|
||||
))
|
||||
print(excel.sheetRename('表一'))
|
||||
print(excel.sheetCreate('表二'))
|
||||
print(excel.sheetCreate('表三'))
|
||||
print(excel.sheetCreate('表四'))
|
||||
print(excel.sheetCreate('表五'))
|
||||
print(excel.sheetCreate('测试'))
|
||||
print(excel.select('测试').sheetDelete())
|
||||
print(excel.sheetList())
|
||||
print(excel.sheetName())
|
||||
print(excel.sheetNrow())
|
||||
print(excel.sheetNcol())
|
||||
print(excel.save())
|
||||
print(excel.save('./example_backup.xlsx'))
|
||||
print(excel.exit())
|
||||
|
|
Loading…
Reference in New Issue