溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2021-06-11 09:44:37 來源:億速云 閱讀:485 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了如何使用python修改excel表某一列內(nèi)容的操作方法,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

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

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

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

1 庫引入

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

import openpyxl
import random

2 提取cell

我們首先提取編號:
編號是第B列

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

print(table['B'])

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

3 提取List

但此時我們發(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)

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

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

接下來我們要找到 ‘工作編號' 這幾個字的位置

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)

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

接下來我們要將最前面的員工名稱復(fù)制到后面,假設(shè)我們已經(jīng)知道有5個人,且知道小標(biā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]
print(IDList)

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

5 修改cell值

這時候我們只需要將只賦回cell即可:

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

這時候卻發(fā)現(xiàn)如下報錯:

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

這時因為我們有的格子是合并起來的

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

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

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

6 存儲回原EXCEL或新EXCEL

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

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

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

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

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

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

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ī)生成工資

與前面類似,較為簡單,建議看完整代碼自己領(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, '工作編號')

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)容的操作方法

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“如何使用python修改excel表某一列內(nèi)容的操作方法”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI