通过无能的baidu逛了一圈,发现有两三段能用的代码,不过参考之下,发现还有不足的:
不能拷贝有合并格式的sheet、没有拷贝cell的相关格式(填充、边框、对齐)等参数
所以通过bing继续发掘,最终合成以下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
from copy import copy from openpyxl import load_workbook, Workbook def replace_xls(src_file,tag_file,sheet_name): # src_file是源xlsx文件,tag_file是目标xlsx文件,sheet_name是目标xlsx里的新sheet名称 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 #最大行数 max_column = ws.max_column #最大列数 wm = zip (ws.merged_cells) #开始处理合并单元格 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) #单元格编号 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单元格数据 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() |
到此这篇关于Python使用openpyxl复制整张sheet的文章就介绍到这了,更多相关Python openpyxl复制sheet内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/d9394952/article/details/88236217