|
合并两个Excel文档:第一个文档如下:第二个文档如下:想要合并成下面图片的样子:学习一段时间了,写了个python合并两个Excel文件的脚本,希望能帮助到像我一样的小白,代码简单,大佬勿喷。不足之处请您不吝赐教:下面代码实现:
* u; z* a) Y) `% T9 j<ol>"""0 P- } K, l; t2 [5 r7 ~5 N5 `. z
Date:2024/10/21 15:46
. m- _( I+ T( c8 G! CFile : MergeExcel.py
" a# ~- j5 G8 J"""
- b/ \6 g- J$ N6 Vfrom copy import copy
8 {. c+ b. I% a& ~. kfrom openpyxl import load_workbook
K7 y$ w R" |% |/ i$ D7 ]2 j+ e! T
1 N1 K5 k5 |+ O# O
class MergeExcel: l: }+ Z, o% z) `# d
def __init__(self):
9 Q/ v3 G! { x* M$ R e1 = "fileDirectory/excel1.xlsx"
; H7 M, W" G2 z+ W% k$ A e2 = "fileDirectory/excel2.xlsx"6 d. F" t: \( l: ~6 ]& t* N
self.wb1 = load_workbook(e1)! W6 U9 h4 P5 f2 G1 g. w0 Q% Z
self.wb2 = load_workbook(e2)
: Z% ?2 N! ?+ H self.sheet1 = self.wb1.active- E8 h G* @- C, F
self.sheet2 = self.wb2.active8 w- Y9 K7 Q& u y! w! M" ?1 e! M
self.insert_row = 99 # 这里设置从哪行插入第一张表的数据 或者 self.sheet2.max_row + 1
0 J* d" K+ \& q4 c+ d g+ [9 M7 K
@staticmethod6 S. b& i1 P; Y' n4 H7 |# ^% V
def copy_style(source_cell, target_cell):
+ S+ t" K" \+ \5 ]- z if source_cell.has_style:) H8 V: C ^' o# [5 K! ^
target_cell.font = copy(source_cell.font) p4 d c6 P8 @& y( k- t
target_cell.fill = copy(source_cell.fill)3 Q$ |0 C' N- g
target_cell.border = copy(source_cell.border). K) w9 N4 q8 J- I+ d+ n
target_cell.alignment = copy(source_cell.alignment)
+ I6 H, L# I% f4 u( G/ L' } target_cell.number_format = source_cell.number_format1 L! R1 }& M. |0 p8 }
target_cell.protection = copy(source_cell.protection)
# J( g% \3 k7 M/ Z y # 使用openpyxl库的cell.comment属性获取单元格的注释。" h( C1 R/ }- ~% H* \3 R" o4 |
if source_cell.comment:
9 w4 X! f" |3 U target_cell.comment = source_cell.comment # 有批注的复制批注5 {+ B( T0 z4 g$ J
# _2 ?4 V5 M+ z1 D' f
def apply_merge_cell(self, merged_ranges):
% ]1 t6 m \; V7 { for min_row, min_col, max_row, max_col in merged_ranges:6 N, d! p4 m9 J
self.sheet2.merge_cells(start_row=min_row, start_column=min_col,
8 L3 n7 F& C1 L5 e2 g0 B6 j% W( w! X* i end_row=max_row, end_column=max_col)
& U# u8 z; s6 b3 F/ b' h) x. P+ r: Q4 W# S( I% G
def copy_value_style(self, source_start_row, source_end_row, source_start_col, source_end_col, target_start_row,5 `& f! ?3 v1 d) s
target_start_col):* Y' W) E/ b3 ~% N ]
for row in range(source_start_row, source_end_row + 1):
, u, x5 x1 }1 L for col in range(source_start_col, source_end_col + 1):! F2 S& {. S' H0 a
source_cell = self.sheet1.cell(row=row, column=col)& K! N9 N" z3 H# {( R% S s
target_cell = self.sheet2.cell(row=row + target_start_row - source_start_row,
0 F, ]( _% |% m' f. k column=col + target_start_col - source_start_col)' s0 I! h- V& v/ f7 o: p# k
self.sheet2.cell(row=row + target_start_row - source_start_row,5 C' }+ p9 \8 }, y0 m
column=col + target_start_col - source_start_col).value = source_cell.value
! t8 q/ F% v( R' A* K3 I; ?
# D+ q4 I+ A: F9 `8 X. S& K self.copy_style(source_cell, target_cell)8 U/ e3 ]6 |: K* F) l5 T; j
4 X2 V( n8 j* C/ O$ f6 ^% _" ? def copy_stats(self, source_start_row, source_end_row, source_start_col, source_end_col, target_start_row,
# {0 m5 R+ r ]* o* V target_start_col):- c$ R t9 K' L* d1 u
merged_ranges = []3 z5 w+ C% q; [8 L
for merge_range in self.sheet1.merged_cells.ranges:" M" R* u3 e6 s% y) J/ R
min_col, min_row, max_col, max_row = merge_range.bounds9 l6 J/ c; O: K- Z4 c4 Q
print(min_row, min_col, max_row, max_col)
5 g. [3 k, h: v print(source_start_row, source_start_col,source_end_row, source_end_col)2 a2 N7 y* ~, R w4 C# h" c1 v! E
if (min_row >= source_start_row and max_row = source_start_col and max_col |
|