Think about how you do forms in a company , Now there's one like this excel The watch is in front of you , This is a monthly salary table for employees ,

Now suppose , What you have to do , Fill in the number of each employee in the next few months , And randomly generate a 2000 reach 50000 A random number between as the month's wages , How much you can take depends on God's will , In order to exercise yourself python Ability decides to write a relevant code ：

<>1 Library introduction

First of all, we need to import the function , To modify excel Content first needs to have openpyxl This library , To generate random numbers, we need to have random This library
import openpyxl import random
<>2 extract cell

Let's first extract the number ：
The number is number one B column
workbook=openpyxl.load_workbook(' wages .xlsx') table = workbook['Sheet1'] print(
table['B'])

<>3 extract List

But at this point we found that what we extracted was cell Format data, not what we usually see list format , We can get it in the following ways list format ：
def cell2List(CELL): LIST=[] for cell in CELL: LIST.append(cell.value) return
LIST IDList=cell2List(table['B']) print(IDList)

<>4 modify List data

Next we need to find out ‘ Job number ’ Where are these words
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, ' Job number ') print(IDPos)

Next, we will copy the name of the first employee to the back , Let's say we already know there are 5 personal , And know that subtitles occupy two spaces （‘ Job number ’ These words are followed by ’ '）
Then write the following code ：
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 modify cell value

At this time, we just need to endow it back cell that will do ：
tempi=0 for cell in table['B']: cell.value=IDList[tempi] tempi=tempi+1
At this time, we found the following error ：

At this time, because some of our lattices are merged

Just change the code to the following form ：
tempi=0 for cell in table['B']: try: cell.value=IDList[tempi] except: print('')
tempi=tempi+1
<>6 Store back to original EXCEL Or new EXCEL

Mainly by changing the following parameters , For example, I want to save a new one result.xlsx
workbook.save(filename = "result.xlsx")
<>7 Other format amendments ( Take the left as an example )

If you find out , At this point, the number of stored results is in the Bureau ：

I want to put it on the left , Just change the previous code to ：
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 Randomly generated wages

Similar to before , It's simple , Suggestions to see the complete code to understand their own Ow

<>9 Complete code
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.
load_workbook(' wages .xlsx') table = workbook['Sheet1'] IDList=cell2List(table['B'])
salaryList=cell2List(table['C']) IDPos=get_location_in_list(IDList, ' Job number ')
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")
effect ：

Technology
Daily Recommendation