最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何用python復(fù)制粘貼excel指定單元格(可保留格式)

 更新時(shí)間:2023年07月14日 09:02:44   作者:鄉(xiāng)村大師i  
這篇文章主要給大家介紹了關(guān)于如何用python復(fù)制粘貼excel指定單元格(可保留格式)的相關(guān)資料,利用python操作excel非常方便,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

近期學(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)文章

最新評(píng)論

若尔盖县| 航空| 望江县| 康平县| 通山县| 古浪县| 绵竹市| 桐庐县| 旬阳县| 深水埗区| 白银市| 平罗县| 旬阳县| 齐齐哈尔市| 达拉特旗| 惠水县| 台中县| 兴山县| 金塔县| 阿克苏市| 苏州市| 扬州市| 南川市| 鹤庆县| 尖扎县| 黄平县| 堆龙德庆县| 连南| 兴安盟| 泽州县| 兴海县| 郴州市| 海盐县| 广西| 阆中市| 巴塘县| 洛阳市| 黎平县| 延吉市| 达拉特旗| 明星|