近期學(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)容。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-536907.html
# 復(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ù)了:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-536907.html
#插入庫(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]
到了這里,關(guān)于用python復(fù)制粘貼excel指定單元格(可保留格式)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!