用python修改excel表某一列內(nèi)容的操作方法
想想你在一家公司里做表格,現(xiàn)在有一個下面這樣的excel表擺在你面前,這是一個員工每個月工資的表,
現(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'])
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)
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)
接下來我們要將最前面的員工名稱復(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)
5 修改cell值
這時候我們只需要將只賦回cell即可:
tempi=0 for cell in table['B']: cell.value=IDList[tempi] tempi=tempi+1
這時候卻發(fā)現(xiàn)如下報錯:
這時因?yàn)槲覀冇械母褡邮呛喜⑵饋淼?/p>
只需要將代碼改成如下形式即可:
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é)果編號局中了:
我想將其居左,只需將前面代碼修改為:
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)容的詳細(xì)內(nèi)容,更多關(guān)于python修改excel的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python3+django2.0+apache2+ubuntu14部署網(wǎng)站上線的方法
這篇文章主要介紹了Python3+django2.0+apache2+ubuntu14部署網(wǎng)站上線的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07Python實(shí)現(xiàn)將json文件生成C語言的結(jié)構(gòu)體的腳本分享
這篇文章主要為大家詳細(xì)介紹了Python如何實(shí)現(xiàn)將json文件生成C語言的結(jié)構(gòu)體,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-09-09python已協(xié)程方式處理任務(wù)實(shí)現(xiàn)過程
這篇文章主要介紹了python已協(xié)程方式處理任務(wù)實(shí)現(xiàn)過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12Python常用模塊之threading和Thread模塊及線程通信
這篇文章主要介紹了Python常用模塊之threading和Thread模塊及線程通信,文章為圍繞主題的相關(guān)內(nèi)容展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友看可以參考一下方法2022-06-06Python使用Tkinter實(shí)現(xiàn)滾動抽獎器效果
Tkinter 是 Python 的標(biāo)準(zhǔn) GUI(Graphical User Interface,圖形用戶接口)庫,Python 使用 Tkinter 可以快速地創(chuàng)建 GUI 應(yīng)用程序。這篇文章主要介紹了Python使用Tkinter實(shí)現(xiàn)滾動抽獎器,需要的朋友可以參考下2020-01-01用Python實(shí)現(xiàn)定時備份Mongodb數(shù)據(jù)并上傳到FTP服務(wù)器
這篇文章主要介紹了用Python實(shí)現(xiàn)定時備份Mongodb數(shù)據(jù)并上傳到FTP服務(wù)器,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01