新闻动态

用python修改excel表某一列内容的操作方法

发布日期:2022-03-13 15:13 | 文章来源:站长之家

想想你在一家公司里做表格,现在有一个下面这样的excel表摆在你面前,这是一个员工每个月工资的表,

现在假设,你要做的事情,是填充好后面几个月每个员工的编号,并且给员工随机生成一个2000到50000之间的随机数作为该月的工资,能拿多少全靠天意,你为了锻炼自己的python能力决定写一个相关的代码:

1 库引入

首先要引入库函数,要修改excel内容首先需要有openpyxl这个库,要生成随机数就要有random这个库

import openpyxl
import random

2 提取cell

我们首先提取编号:
编号是第B列

workbook=openpyxl.load_workbook('工资.xlsx')
table = workbook['Sheet1']
print(table['B'])

3 提取List

但此时我们发现提取出的是cell格式的数据而不是我们常见的list格式,我们可以通过以下方式获得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)

接下来我们要将最前面的员工名称复制到后面,假设我们已经知道有5个人,且知道小标题占两个格子(‘工作编号' 这几个字后面跟着' ')
那么编写如下代码:

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即可:

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

主要靠更改后面参数,例如我想新存一个result.xlsx

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.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的资料请关注本站其它相关文章!

美国快速服务器

版权声明:本站文章来源标注为YINGSOO的内容版权均为本站所有,欢迎引用、转载,请保持原文完整并注明来源及原文链接。禁止复制或仿造本网站,禁止在非www.yingsoo.com所属的服务器上建立镜像,否则将依法追究法律责任。本站部分内容来源于网友推荐、互联网收集整理而来,仅供学习参考,不代表本站立场,如有内容涉嫌侵权,请联系alex-e#qq.com处理。

相关文章

实时开通

自选配置、实时开通

免备案

全球线路精选!

全天候客户服务

7x24全年不间断在线

专属顾问服务

1对1客户咨询顾问

在线
客服

在线客服:7*24小时在线

客服
热线

400-630-3752
7*24小时客服服务热线

关注
微信

关注官方微信
顶部