想想你在一家公司里做表格,现在有一个下面这样的excel表摆在你面前,这是一个员工每个月工资的表,
现在假设,你要做的事情,是填充好后面几个月每个员工的编号,并且给员工随机生成一个2000到50000之间的随机数作为该月的工资,能拿多少全靠天意,你为了锻炼自己的python能力决定写一个相关的代码:
1 库引入
首先要引入库函数,要修改excel内容首先需要有openpyxl这个库,要生成随机数就要有random这个库
1
2
|
import openpyxl
import random
|
2 提取cell
我们首先提取编号:
编号是第b列
1
2
3
4
|
workbook = openpyxl.load_workbook( '工资.xlsx' )
table = workbook[ 'sheet1' ]
print (table[ 'b' ])
|
3 提取list
但此时我们发现提取出的是cell格式的数据而不是我们常见的list格式,我们可以通过以下方式获得list格式:
1
2
3
4
5
6
7
8
|
def cell2list(cell):
list = []
for cell in cell:
list .append(cell.value)
return list
idlist = cell2list(table[ 'b' ])
print (idlist)
|
4 修改list数据
接下来我们要找到 ‘工作编号' 这几个字的位置
1
2
3
4
5
6
7
8
9
10
11
|
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)
|
接下来我们要将最前面的员工名称复制到后面,假设我们已经知道有5个人,且知道小标题占两个格子(‘工作编号' 这几个字后面跟着' ')
那么编写如下代码:
1
2
3
4
|
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值
这时候我们只需要将只赋回cell即可:
1
2
3
4
|
tempi = 0
for cell in table[ 'b' ]:
cell.value = idlist[tempi]
tempi = tempi + 1
|
这时候却发现如下报错:
这时因为我们有的格子是合并起来的
只需要将代码改成如下形式即可:
1
2
3
4
5
6
7
|
tempi = 0
for cell in table[ 'b' ]:
try :
cell.value = idlist[tempi]
except :
print ('')
tempi = tempi + 1
|
6 存储回原excel或新excel
主要靠更改后面参数,例如我想新存一个result.xlsx
1
|
workbook.save(filename = "result.xlsx" )
|
7 其他格式修正(居左为例)
假如你发现,此时存储结果编号局中了:
我想将其居左,只需将前面代码修改为:
1
2
3
4
5
6
7
8
|
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 完整代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
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( '工资.xlsx' )
table = workbook[ 'sheet1' ]
idlist = cell2list(table[ 'b' ])
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" )
|
效果:
以上就是用python修改excel表某一列内容的详细内容,更多关于python修改excel的资料请关注服务器之家其它相关文章!
原文链接:https://blog.csdn.net/slandarer/article/details/117777396