如何用python復(fù)制粘貼excel指定單元格(可保留格式)
近期學習了openpyxl的用法,發(fā)現(xiàn)居然沒有【復(fù)制、粘貼】這么基礎(chǔ)的函數(shù)。而且若要用python帶格式復(fù)制粘貼指定區(qū)域的單元格,參考資料更少。
于是參考各路大佬的筆記,整合如下。
本代碼只完成一次復(fù)制粘貼,各位可根據(jù)自己的需要加以利用,比如:可搭配遍歷文件等實現(xiàn)多個excel中指定區(qū)域的復(fù)制,并匯總于指定區(qū)域的內(nèi)容。
# 復(fù)制區(qū)域cell、帶格式粘貼: 比如把a1:f16帶格式復(fù)制粘貼到h23:m38
#導(dǎo)入包
import openpyxl
import copy
#path單引號內(nèi)放入指定要操作的excel的路徑 (本文舉例的復(fù)制與粘貼,位于同一excel的同一sheet)
path = r'E:\OneDrive\Python_Note\Excel操作\03\反饋表-小寨支行.xlsx'
wb = openpyxl.load_workbook(path)
ws = wb.active #本行代碼意思是指定ws為當前在excel中處于選中狀態(tài)的sheet為ws。
#若excel內(nèi)有多個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) #提取出單元格編號的字符串,如'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) # 提取出單元格編號的字符串,如'L10'
print('target_cell_list:',target_cell_list)
#獲取要復(fù)制的單元格總個數(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')再補充一下如何遍歷到某個文件夾下所有的excel。這樣就可以實現(xiàn)批量自動提取每個excel的指定位置的數(shù)據(jù)了:
#插入庫(同樣庫只用插入一遍)
import openpyxl
import os
#指定文件夾路徑:
path = r'E:\OneDrive\Python_Note\Excel操作'
#使用for循環(huán)對文件夾內(nèi)的每個excel進行相同操作:
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)
#以下開始是在每個excel內(nèi)的所需操作(根據(jù)自己需要)
ws = wb.active #指定sheet名,推薦操作的excel都只有一個sheet時用,可以解決各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實現(xiàn)word 2007文檔轉(zhuǎn)換為pdf文件
這篇文章主要為大家詳細介紹了python實現(xiàn)word 2007文檔轉(zhuǎn)換為pdf文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
Python3安裝requests庫的步驟及使用示例和常見問題
Python3的requests庫是處理HTTP請求最常用的工具之一,它簡化了與Web服務(wù)的交互,讓開發(fā)者能更專注于業(yè)務(wù)邏輯,而非底層網(wǎng)絡(luò)細節(jié),這篇文章主要介紹了Python3安裝requests庫的步驟及使用示例和常見問題,需要的朋友可以參考下2026-06-06
使用Python編寫一個在Linux下實現(xiàn)截圖分享的腳本的教程
這篇文章主要介紹了使用Python編寫一個在Linux下實現(xiàn)截圖分享的腳本的教程,利用到了scrot和urllib2庫,需要的朋友可以參考下2015-04-04
python庫pycryptodom加密技術(shù)探索(公鑰加密私鑰加密)
這篇文章主要為大家介紹了python庫pycryptodom加密技術(shù)探索(公鑰加密私鑰加密),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01
解決安裝pycharm后不能執(zhí)行python腳本的問題
今天小編就為大家分享一篇解決安裝pycharm后不能執(zhí)行python腳本的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
python實現(xiàn)人臉識別經(jīng)典算法(一) 特征臉法
這篇文章主要為大家詳細介紹了python實現(xiàn)人臉識別經(jīng)典算法,特征臉法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03

