<>1 库引入

import openpyxl import random
<>2 提取cell

table['B'])

<>3 提取List

def cell2List(CELL): LIST=[] for cell in CELL: LIST.append(cell.value) return
LIST IDList=cell2List(table['B']) print(IDList)

<>4 修改List数据

def get_location_in_list(x, target): step = -1 items = list() for i in range(x.
count(target)): y = x[step + 1:].index(target) step = step + y + 1 items.append(
step) return items IDPos=get_location_in_list(IDList, '工作编号') print(IDPos)

staffNum=5 for i in range(0,len(IDPos)): IDList[IDPos[i]+1:IDPos[i]+2+staffNum]
=IDList[IDPos[0]+1:IDPos[0]+2+staffNum] print(IDList)

<>5 修改cell值

tempi=0 for cell in table['B']: cell.value=IDList[tempi] tempi=tempi+1

tempi=0 for cell in table['B']: try: cell.value=IDList[tempi] except: print('')
tempi=tempi+1
<>6 存储回原EXCEL或新EXCEL

workbook.save(filename = "result.xlsx")
<>7 其他格式修正(居左为例)

tempi=0 for cell in table['B']: try: cell.value=IDList[tempi] cell.alignment =
openpyxl.styles.Alignment(horizontal='left', vertical='center') except: print(''
) tempi=tempi+1
<>8 随机生成工资

<>9 完整代码
import openpyxl import random def cell2List(CELL): LIST=[] for cell in CELL:
LIST.append(cell.value) return LIST def get_location_in_list(x, target): step =
-1 items = list() for i in range(x.count(target)): y = x[step + 1:].index(target
) step = step + y + 1 items.append(step) return items workbook=openpyxl.
salaryList=cell2List(table['C']) IDPos=get_location_in_list(IDList, '工作编号')
staffNum=5 for i in range(0,len(IDPos)): IDList[IDPos[i]+1:IDPos[i]+2+staffNum]=
IDList[IDPos[0]+1:IDPos[0]+2+staffNum] for j in range(IDPos[i]+1,IDPos[i]+2+
staffNum): salaryList[j]=1 # tempi=0 # for cell in table['B']: #
cell.value=IDList[tempi] # tempi=tempi+1 tempi=0 for cell in table['B']: try:
cell.value=IDList[tempi] cell.alignment = openpyxl.styles.Alignment(horizontal=
'left', vertical='center') except: print('') tempi=tempi+1 tempi=0 for cell in
table['C']: try: if salaryList[tempi]==1: cell.value=random.randint(2000,50000)
except: print('') tempi=tempi+1 workbook.save(filename = "result.xlsx")