人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

用python修改excel表某一列內(nèi)容的操作方法

發(fā)布日期:2022-03-13 15:13 | 文章來(lái)源:站長(zhǎng)之家

想想你在一家公司里做表格,現(xiàn)在有一個(gè)下面這樣的excel表擺在你面前,這是一個(gè)員工每個(gè)月工資的表,

現(xiàn)在假設(shè),你要做的事情,是填充好后面幾個(gè)月每個(gè)員工的編號(hào),并且給員工隨機(jī)生成一個(gè)2000到50000之間的隨機(jī)數(shù)作為該月的工資,能拿多少全靠天意,你為了鍛煉自己的python能力決定寫一個(gè)相關(guān)的代碼:

1 庫(kù)引入

首先要引入庫(kù)函數(shù),要修改excel內(nèi)容首先需要有openpyxl這個(gè)庫(kù),要生成隨機(jī)數(shù)就要有random這個(gè)庫(kù)

import openpyxl
import random

2 提取cell

我們首先提取編號(hào):
編號(hào)是第B列

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

3 提取List

但此時(shí)我們發(fā)現(xiàn)提取出的是cell格式的數(shù)據(jù)而不是我們常見的list格式,我們可以通過以下方式獲得list格式:

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

4 修改List數(shù)據(jù)

接下來(lái)我們要找到 ‘工作編號(hào)' 這幾個(gè)字的位置

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, '工作編號(hào)')
print(IDPos)

接下來(lái)我們要將最前面的員工名稱復(fù)制到后面,假設(shè)我們已經(jīng)知道有5個(gè)人,且知道小標(biāo)題占兩個(gè)格子(‘工作編號(hào)' 這幾個(gè)字后面跟著' ')
那么編寫如下代碼:

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值

這時(shí)候我們只需要將只賦回cell即可:

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

這時(shí)候卻發(fā)現(xiàn)如下報(bào)錯(cuò):

這時(shí)因?yàn)槲覀冇械母褡邮呛喜⑵饋?lái)的

只需要將代碼改成如下形式即可:

tempi=0
for cell in table['B']:
 try:
  cell.value=IDList[tempi]
 except:
  print('')
 tempi=tempi+1

6 存儲(chǔ)回原EXCEL或新EXCEL

主要靠更改后面參數(shù),例如我想新存一個(gè)result.xlsx

workbook.save(filename = "result.xlsx")

7 其他格式修正(居左為例)

假如你發(fā)現(xiàn),此時(shí)存儲(chǔ)結(jié)果編號(hào)局中了:

我想將其居左,只需將前面代碼修改為:

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 隨機(jī)生成工資

與前面類似,較為簡(jiǎn)單,建議看完整代碼自己領(lǐng)悟嗷

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, '工作編號(hào)')
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表某一列內(nèi)容的詳細(xì)內(nèi)容,更多關(guān)于python修改excel的資料請(qǐng)關(guān)注本站其它相關(guān)文章!

美國(guó)快速服務(wù)器

版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開通

自選配置、實(shí)時(shí)開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對(duì)1客戶咨詢顧問

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部