Python使用openpyxl復(fù)制整張sheet
通過(guò)無(wú)能的baidu逛了一圈,發(fā)現(xiàn)有兩三段能用的代碼,不過(guò)參考之下,發(fā)現(xiàn)還有不足的:
不能拷貝有合并格式的sheet、沒(méi)有拷貝cell的相關(guān)格式(填充、邊框、對(duì)齊)等參數(shù)
所以通過(guò)bing繼續(xù)發(fā)掘,最終合成以下代碼:
from copy import copy
from openpyxl import load_workbook, Workbook
def replace_xls(src_file,tag_file,sheet_name):
# src_file是源xlsx文件,tag_file是目標(biāo)xlsx文件,sheet_name是目標(biāo)xlsx里的新sheet名稱(chēng)
print("Start sheet %s copy from %s to %s"%(sheet_name,src_file,tag_file))
wb = load_workbook(src_file)
wb2 = load_workbook(tag_file)
ws = wb.get_sheet_by_name(wb.get_sheet_names()[0])
ws2 = wb2.create_sheet(sheet_name.decode('utf-8'))
max_row=ws.max_row #最大行數(shù)
max_column=ws.max_column #最大列數(shù)
wm=zip(ws.merged_cells) #開(kāi)始處理合并單元格
if len(wm)>0 :
for i in range(0,len(wm)):
cell2=str(wm[i]).replace('(<MergeCell ','').replace('>,)','')
print("MergeCell : %s" % cell2)
ws2.merge_cells(cell2)
for m in range(1,max_row + 1):
ws2.row_dimensions[m].height = ws.row_dimensions[m].height
for n in range(1,1 + max_column):
if n<27 :
c=chr(n+64).upper() #ASCII字符,chr(65)='A'
else:
if n < 677 :
c=chr(divmod(n,26)[0]+64)+chr(divmod(n,26)[1]+64)
else:
c=chr(divmod(n,676)[0]+64) + chr(divmod(divmod(n,676)[1],26)[0]+64) + chr(divmod(divmod(n,676)[1],26)[1]+64)
i='%s%d'%(c,m) #單元格編號(hào)
if m == 1 :
# print("Modify column %s width from %d to %d" % (n, ws2.column_dimensions[c].width ,ws.column_dimensions[c].width))
ws2.column_dimensions[c].width = ws.column_dimensions[c].width
try:
getattr(ws.cell(row=m, column=c), "value" )
cell1=ws[i] #獲取data單元格數(shù)據(jù)
ws2[i].value=cell1.value #賦值到ws2單元格
if cell1.has_style: #拷貝格式
ws2[i].font = copy(cell1.font)
ws2[i].border = copy(cell1.border)
ws2[i].fill = copy(cell1.fill)
ws2[i].number_format = copy(cell1.number_format)
ws2[i].protection = copy(cell1.protection)
ws2[i].alignment = copy(cell1.alignment)
except AttributeError as e:
print("cell(%s) is %s" % (i,e))
continue
wb2.save(tag_file)
wb2.close()
wb.close()
到此這篇關(guān)于Python使用openpyxl復(fù)制整張sheet的文章就介紹到這了,更多相關(guān)Python openpyxl復(fù)制sheet內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)檢測(cè)服務(wù)器是否可以ping通的2種方法
這篇文章主要介紹了Python實(shí)現(xiàn)檢測(cè)服務(wù)器是否可以ping通的2種方法,本文分別講解了使用ping和fping命令檢測(cè)服務(wù)器是否可以ping通,需要的朋友可以參考下2015-01-01
Python基于遞歸算法實(shí)現(xiàn)的走迷宮問(wèn)題
這篇文章主要介紹了Python基于遞歸算法實(shí)現(xiàn)的走迷宮問(wèn)題,結(jié)合迷宮問(wèn)題簡(jiǎn)單分析了Python遞歸算法的定義與使用技巧,需要的朋友可以參考下2017-08-08
TensorFlow人工智能學(xué)習(xí)數(shù)據(jù)合并分割統(tǒng)計(jì)示例詳解
這篇文章主要為大家介紹了TensorFlow人工智能學(xué)習(xí)數(shù)據(jù)合并分割及統(tǒng)計(jì)的示例詳解有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
解決Pandas to_json()中文亂碼,轉(zhuǎn)化為json數(shù)組的問(wèn)題
今天小編就為大家分享一篇解決Pandas to_json() 中文亂碼,轉(zhuǎn)化為json數(shù)組的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
一文搞懂Pandas數(shù)據(jù)透視的4個(gè)函數(shù)的使用
今天主要和大家分享Pandas中四種有關(guān)數(shù)據(jù)透視的通用函數(shù),在數(shù)據(jù)處理中遇到這類(lèi)需求時(shí),能夠很好地應(yīng)對(duì),快跟隨小編一起學(xué)習(xí)一下吧2022-06-06
Python unittest生成測(cè)試報(bào)告過(guò)程解析
這篇文章主要介紹了Python unittest生成測(cè)試報(bào)告過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
基于Python實(shí)現(xiàn)體育彩票選號(hào)器功能代碼實(shí)例
這篇文章主要介紹了基于Python實(shí)現(xiàn)體育彩票選號(hào)器功能代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09

