如何用python復(fù)制粘貼excel指定單元格(可保留格式)
近期學(xué)習(xí)了openpyxl的用法,發(fā)現(xiàn)居然沒有【復(fù)制、粘貼】這么基礎(chǔ)的函數(shù)。而且若要用python帶格式復(fù)制粘貼指定區(qū)域的單元格,參考資料更少。
于是參考各路大佬的筆記,整合如下。
本代碼只完成一次復(fù)制粘貼,各位可根據(jù)自己的需要加以利用,比如:可搭配遍歷文件等實(shí)現(xiàn)多個(gè)excel中指定區(qū)域的復(fù)制,并匯總于指定區(qū)域的內(nèi)容。
# 復(fù)制區(qū)域cell、帶格式粘貼: 比如把a(bǔ)1:f16帶格式復(fù)制粘貼到h23:m38 #導(dǎo)入包 import openpyxl import copy #path單引號(hào)內(nèi)放入指定要操作的excel的路徑 (本文舉例的復(fù)制與粘貼,位于同一excel的同一sheet) path = r'E:\OneDrive\Python_Note\Excel操作\03\反饋表-小寨支行.xlsx' wb = openpyxl.load_workbook(path) ws = wb.active #本行代碼意思是指定ws為當(dāng)前在excel中處于選中狀態(tài)的sheet為ws。 #若excel內(nèi)有多個(gè)sheet,建議使用ws=wb['sheet的名字'] #以字符串輸入復(fù)制、粘貼的區(qū)域,如'a1:f16','h23:m38'(必須大小一致) Source_Area = 'a1:f16' Target_Area = 'h23:m38' #分別指定復(fù)制和粘貼所在sheet的位置(本文復(fù)制粘貼的單元格區(qū)域都在ws內(nèi),ws是什么在上面已經(jīng)指定好) source_area = ws[Source_Area] target_area = ws[Target_Area] #創(chuàng)造source_cell_list,用以和target_cell_list一一對應(yīng): source_cell_list = [] for source_row in source_area: for source_cell in source_row: sc_str = str(source_cell) point_time = sc_str.count('.') sc_str = sc_str.replace('.', '', point_time - 1) start = sc_str.find('.') sc_str = sc_str[start+1 : -1] source_cell_list.append(sc_str) #提取出單元格編號(hào)的字符串,如'C8' print('source_cell_list:',source_cell_list) target_cell_list = [] for target_row in target_area: for target_cell in target_row: tc_str = str(target_cell) point_time = tc_str.count('.') tc_str = tc_str.replace('.', '', point_time - 1) start = tc_str.find('.') tc_str = tc_str[start + 1: -1] target_cell_list.append(tc_str) # 提取出單元格編號(hào)的字符串,如'L10' print('target_cell_list:',target_cell_list) #獲取要復(fù)制的單元格總個(gè)數(shù): cells = len(source_cell_list) #提取并復(fù)制格式: i=0 while i<=cells-1: ws[target_cell_list[0+i]].data_type = ws[source_cell_list[0+i]].data_type if ws[source_cell_list[0+i]].has_style: ws[target_cell_list[0+i]]._style = copy.copy(ws[source_cell_list[0+i]]._style) ws[target_cell_list[0+i]].font = copy.copy(ws[source_cell_list[0+i]].font) ws[target_cell_list[0+i]].border = copy.copy(ws[source_cell_list[0+i]].border) ws[target_cell_list[0+i]].fill = copy.copy(ws[source_cell_list[0+i]].fill) ws[target_cell_list[0+i]].number_format = copy.copy(ws[source_cell_list[0+i]].number_format) ws[target_cell_list[0+i]].protection = copy.copy(ws[source_cell_list[0+i]].protection) ws[target_cell_list[0+i]].alignment = copy.copy(ws[source_cell_list[0+i]].alignment) # 通過引用方法粘貼值: ws['']=ws[''].value ws[target_cell_list[0+i]] = ws[source_cell_list[0+i]].value i+=1 #保存更改:(若復(fù)制粘貼來源于不同文件要分別保存) wb.save(r'E:\OneDrive\Python_Note\Excel操作\03\反饋表-小寨支行-py改.xlsx')
再補(bǔ)充一下如何遍歷到某個(gè)文件夾下所有的excel。這樣就可以實(shí)現(xiàn)批量自動(dòng)提取每個(gè)excel的指定位置的數(shù)據(jù)了:
#插入庫(同樣庫只用插入一遍) import openpyxl import os #指定文件夾路徑: path = r'E:\OneDrive\Python_Note\Excel操作' #使用for循環(huán)對文件夾內(nèi)的每個(gè)excel進(jìn)行相同操作: for file_name in os.listdir(path): if file_name.endswith('.xlsx') or file_name.endswith(".xls"): #本次循環(huán)對象excel文件的絕對路徑: excel = path + '\\' + file_name print('開始執(zhí)行:',excel) wb = openpyxl.load_workbook(excel) #以下開始是在每個(gè)excel內(nèi)的所需操作(根據(jù)自己需要) ws = wb.active #指定sheet名,推薦操作的excel都只有一個(gè)sheet時(shí)用,可以解決各excel里sheet名稱不同的問題。 Source_Area = 'a1:f16' Target_Area = 'h23:m38' source_area = ws[Source_Area] target_area = ws[Target_Area]
總結(jié)
到此這篇關(guān)于如何用python復(fù)制粘貼excel指定單元格的文章就介紹到這了,更多相關(guān)python復(fù)制粘貼excel指定單元格內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用python進(jìn)行矩陣運(yùn)算實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于如何利用python進(jìn)行矩陣運(yùn)算的相關(guān)資料,Numpy是Python編程語言中的一個(gè)核心庫,專門用于處理多維數(shù)據(jù)和矩陣運(yùn)算,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-12-12Python使用turtle繪制有趣的龍年祝福動(dòng)畫
這篇文章主要介紹了Python的內(nèi)置庫——小海龜(turtle),它是一個(gè)非常實(shí)用的繪畫工具,不僅可以幫助我們繪制圖形,還能讓我們查看整個(gè)繪畫過程,下面我們就來看看如何使用turtle繪制有趣的龍年祝福動(dòng)畫吧2024-01-01conda管理Python虛擬環(huán)境的實(shí)現(xiàn)
本文主要介紹了conda管理Python虛擬環(huán)境的實(shí)現(xiàn),主要包括使用conda工具創(chuàng)建、查看和刪除Python虛擬環(huán)境,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01Django REST framework 分頁的實(shí)現(xiàn)代碼
這篇文章主要介紹了Django REST framework 分頁的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06分享Pandas庫中的一些寶藏函數(shù)transform()
Pandas具有很多強(qiáng)大的功能,transform就是其中之一,利用它可以高效地匯總數(shù)據(jù)且不改變數(shù)據(jù)行數(shù),transform是一種什么數(shù)據(jù)操作?如果熟悉SQL的窗口函數(shù),就非常容易理解了2021-09-09淺析Python中g(shù)lobal和nonlocal關(guān)鍵字的妙用
這篇文章主要來和大家一起深入探討Python中關(guān)鍵詞global和nonlocal的用法,包括詳細(xì)的示例代碼和實(shí)際應(yīng)用場景,感興趣的可以了解下2024-04-04Python實(shí)現(xiàn)的在特定目錄下導(dǎo)入模塊功能分析
這篇文章主要介紹了Python實(shí)現(xiàn)的在特定目錄下導(dǎo)入模塊功能,結(jié)合實(shí)例形式分析了Python基于系統(tǒng)函數(shù)及import語句實(shí)現(xiàn)模塊導(dǎo)入的相關(guān)操作技巧,需要的朋友可以參考下2019-02-02libreoffice python 操作word及excel文檔的方法
這篇文章主要介紹了libreoffice python 操作word及excel文檔的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07