如何用python復(fù)制粘貼excel指定單元格(可保留格式)
近期學(xué)習(xí)了openpyxl的用法,發(fā)現(xiàn)居然沒(méi)有【復(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一一對(duì)應(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)
# 通過(guò)引用方法粘貼值: ws['']=ws[''].value
ws[target_cell_list[0+i]] = ws[source_cell_list[0+i]].value
i+=1
#保存更改:(若復(fù)制粘貼來(lái)源于不同文件要分別保存)
wb.save(r'E:\OneDrive\Python_Note\Excel操作\03\反饋表-小寨支行-py改.xlsx')再補(bǔ)充一下如何遍歷到某個(gè)文件夾下所有的excel。這樣就可以實(shí)現(xiàn)批量自動(dòng)提取每個(gè)excel的指定位置的數(shù)據(jù)了:
#插入庫(kù)(同樣庫(kù)只用插入一遍)
import openpyxl
import os
#指定文件夾路徑:
path = r'E:\OneDrive\Python_Note\Excel操作'
#使用for循環(huán)對(duì)文件夾內(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)對(duì)象excel文件的絕對(duì)路徑:
excel = path + '\\' + file_name
print('開(kāi)始執(zhí)行:',excel)
wb = openpyxl.load_workbook(excel)
#以下開(kāi)始是在每個(gè)excel內(nèi)的所需操作(根據(jù)自己需要)
ws = wb.active #指定sheet名,推薦操作的excel都只有一個(gè)sheet時(shí)用,可以解決各excel里sheet名稱不同的問(wèn)題。
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)word 2007文檔轉(zhuǎn)換為pdf文件
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)word 2007文檔轉(zhuǎn)換為pdf文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Python3安裝requests庫(kù)的步驟及使用示例和常見(jiàn)問(wèn)題
Python3的requests庫(kù)是處理HTTP請(qǐng)求最常用的工具之一,它簡(jiǎn)化了與Web服務(wù)的交互,讓開(kāi)發(fā)者能更專注于業(yè)務(wù)邏輯,而非底層網(wǎng)絡(luò)細(xì)節(jié),這篇文章主要介紹了Python3安裝requests庫(kù)的步驟及使用示例和常見(jiàn)問(wèn)題,需要的朋友可以參考下2026-06-06
解讀Pandas和Polars的區(qū)別及說(shuō)明
Pandas和Polars是Python中用于數(shù)據(jù)處理的兩個(gè)庫(kù),Pandas適用于中小規(guī)模數(shù)據(jù)的快速原型開(kāi)發(fā)和復(fù)雜數(shù)據(jù)操作,而Polars則專注于高效數(shù)據(jù)處理,支持大規(guī)模數(shù)據(jù)和高性能計(jì)算2025-02-02
使用Python編寫一個(gè)在Linux下實(shí)現(xiàn)截圖分享的腳本的教程
這篇文章主要介紹了使用Python編寫一個(gè)在Linux下實(shí)現(xiàn)截圖分享的腳本的教程,利用到了scrot和urllib2庫(kù),需要的朋友可以參考下2015-04-04
python庫(kù)pycryptodom加密技術(shù)探索(公鑰加密私鑰加密)
這篇文章主要為大家介紹了python庫(kù)pycryptodom加密技術(shù)探索(公鑰加密私鑰加密),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
解決安裝pycharm后不能執(zhí)行python腳本的問(wèn)題
今天小編就為大家分享一篇解決安裝pycharm后不能執(zhí)行python腳本的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Python yield生成器和return對(duì)比代碼實(shí)例
這篇文章主要介紹了Python yield生成器和return對(duì)比代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04

