用python修改excel表某一列內(nèi)容的操作方法
想想你在一家公司里做表格,現(xiàn)在有一個(gè)下面這樣的excel表擺在你面前,這是一個(gè)員工每個(gè)月工資的表,

現(xiàn)在假設(shè),你要做的事情,是填充好后面幾個(gè)月每個(gè)員工的編號(hào),并且給員工隨機(jī)生成一個(gè)2000到50000之間的隨機(jī)數(shù)作為該月的工資,能拿多少全靠天意,你為了鍛煉自己的python能力決定寫一個(gè)相關(guān)的代碼:
1 庫引入
首先要引入庫函數(shù),要修改excel內(nèi)容首先需要有openpyxl這個(gè)庫,要生成隨機(jī)數(shù)就要有random這個(gè)庫
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ù)
接下來我們要找到 ‘工作編號(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)

接下來我們要將最前面的員工名稱復(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)槲覀冇械母褡邮呛喜⑵饋淼?/p>

只需要將代碼改成如下形式即可:
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ān)文章
關(guān)于Python的一些學(xué)習(xí)總結(jié)
這篇文章主要介紹了關(guān)于Python的一些總結(jié),希望自己以后在學(xué)習(xí)Python的過程中可以邊學(xué)習(xí)邊總結(jié),就自己之前的學(xué)習(xí)先做以總結(jié),之后將不斷總結(jié)更新2018-05-05
python加密解密庫cryptography使用openSSL生成的密匙加密解密
這篇文章主要介紹了python加密解密庫cryptography使用openSSL生成的密匙加密解密,需要的朋友可以參考下2020-02-02
mac PyCharm添加Python解釋器及添加package路徑的方法
今天小編就為大家分享一篇mac PyCharm添加Python解釋器及添加package路徑的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Pytorch 抽取vgg各層并進(jìn)行定制化處理的方法
今天小編就為大家分享一篇Pytorch 抽取vgg各層并進(jìn)行定制化處理的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Python實(shí)現(xiàn)excel轉(zhuǎn)sqlite的方法
這篇文章主要介紹了Python實(shí)現(xiàn)excel轉(zhuǎn)sqlite的方法,結(jié)合實(shí)例形式分析了Python基于第三方庫xlrd讀取Excel文件及寫入sqlite的相關(guān)操作技巧,需要的朋友可以參考下2017-07-07
Python解決ModuleNotFoundError: No module named&
ModuleNotFoundError: No module named 'PIL'是一個(gè)常見的Python錯(cuò)誤,通常出現(xiàn)在使用Pillow庫時(shí),Pillow是Python中用于圖像處理的一個(gè)庫,其前身是PIL,本文介紹了Python解決ModuleNotFoundError: No module named 'PIL'的問題,需要的朋友可以參考下2024-09-09
Python中字符串的基礎(chǔ)介紹及常用操作總結(jié)
字符串輸出格式與輸入的樣子相同,都是用撇號(hào)包裹,撇號(hào)和其它特殊字符用用反斜杠轉(zhuǎn)義。如果字符串中有單撇號(hào)而沒有雙撇號(hào)則用雙撇號(hào)包裹,否則應(yīng)該用單撇號(hào)包裹。后面要介紹的print語句可以不帶撇號(hào)或轉(zhuǎn)義輸出字符串2021-09-09

