|
合并两个Excel文档:第一个文档如下:第二个文档如下:想要合并成下面图片的样子:学习一段时间了,写了个python合并两个Excel文件的脚本,希望能帮助到像我一样的小白,代码简单,大佬勿喷。不足之处请您不吝赐教:下面代码实现:
+ z8 B. c& D# Q9 l<ol>"""! }$ b$ p- N4 H
Date:2024/10/21 15:461 W" s; c2 I3 m& G8 s
File : MergeExcel.py
1 d( i2 y6 Y' V( T; _4 I- b"""
' a. c0 y9 h1 i8 R8 efrom copy import copy- S1 N3 ~8 W6 z, W. Q" x
from openpyxl import load_workbook; K& H' u1 |' M! Z% N; u( T
. y7 F1 A/ v: |) p2 K0 Q6 h8 }' Q2 K- G* K p/ w* \7 b
class MergeExcel:
# C$ P) Z$ I0 ~" X def __init__(self):; h8 T M8 E: u' V3 Q# |" ^8 o
e1 = "fileDirectory/excel1.xlsx"9 @: {, E2 ~( A9 s% {
e2 = "fileDirectory/excel2.xlsx"
) h: J+ f8 y9 g; C self.wb1 = load_workbook(e1)5 Y+ z# U3 Y0 ?; l* ^4 e
self.wb2 = load_workbook(e2)
1 Z: U! ^- s( C self.sheet1 = self.wb1.active
7 l, ^9 F) b. p- v3 u self.sheet2 = self.wb2.active
, Z4 }1 ~) A/ a3 C6 }7 s( g self.insert_row = 99 # 这里设置从哪行插入第一张表的数据 或者 self.sheet2.max_row + 1# G* B4 s% |7 x
6 Z8 r. P' Q, j% t+ u: h, c7 M
@staticmethod' Z- N+ L6 Y- z5 ], Y3 U' ]
def copy_style(source_cell, target_cell):
* H4 l: x( \4 _: L6 }0 E if source_cell.has_style:9 b2 C1 E* J; V0 Y* e. }
target_cell.font = copy(source_cell.font)1 X8 N( G( J4 p6 U; X, Y1 g
target_cell.fill = copy(source_cell.fill) l# {' u9 {. u: ~
target_cell.border = copy(source_cell.border)
`" F1 L' W$ g/ Y5 j( b8 [; N, Y target_cell.alignment = copy(source_cell.alignment)" S4 ]' _, f7 x0 F1 @' T
target_cell.number_format = source_cell.number_format+ e' P+ M+ K( m3 a9 e# i2 N
target_cell.protection = copy(source_cell.protection)3 w5 Y# Q0 P. n4 b* H1 B
# 使用openpyxl库的cell.comment属性获取单元格的注释。
. Z p" q. [* q0 U3 G if source_cell.comment:* C1 s% y6 I1 k% Z9 f2 J
target_cell.comment = source_cell.comment # 有批注的复制批注; L3 n: \3 F* [0 W5 Z
: n7 Z! S& | W7 N L! b! ]( h( y def apply_merge_cell(self, merged_ranges):
. F2 ~9 g d! E0 k6 `5 _& X for min_row, min_col, max_row, max_col in merged_ranges:
2 G I" A6 B0 R self.sheet2.merge_cells(start_row=min_row, start_column=min_col,
5 I2 l5 g% Y2 Y% ~1 i end_row=max_row, end_column=max_col)
8 O7 r6 E% z2 T9 E Z+ F# l+ \1 `2 G# X: G/ ~2 k! `
def copy_value_style(self, source_start_row, source_end_row, source_start_col, source_end_col, target_start_row,
' d3 k: X- G6 y9 d1 `2 ~/ z- T target_start_col):
, G: l2 \" I' _+ p$ t1 v, `8 M' K for row in range(source_start_row, source_end_row + 1):3 K5 W J# a( Z( [% j
for col in range(source_start_col, source_end_col + 1):" h+ @' y) Q( W- G+ ~
source_cell = self.sheet1.cell(row=row, column=col)
* K; w7 H& ~, G, m1 x* [ target_cell = self.sheet2.cell(row=row + target_start_row - source_start_row,; D! S- ?. {: s6 S+ ~% e& T7 Y$ Z# m
column=col + target_start_col - source_start_col), @7 q% u* S+ A9 ?+ V' F
self.sheet2.cell(row=row + target_start_row - source_start_row,5 R* O' I( Z# j$ l
column=col + target_start_col - source_start_col).value = source_cell.value
, @2 F1 H' J% K+ n V, r
! g7 z/ Y7 h( ]6 y& w" s. O self.copy_style(source_cell, target_cell)
! W& y4 o% B$ U8 J" G8 [8 ]3 \, t$ y1 N
def copy_stats(self, source_start_row, source_end_row, source_start_col, source_end_col, target_start_row,
: m0 x: q, o+ z! J8 p7 W target_start_col):
( c. F9 X$ o1 V5 o' w7 B merged_ranges = []0 Y& h; Z6 _" P* [/ i- s! K
for merge_range in self.sheet1.merged_cells.ranges:
. H# N( |" L9 k# B! R6 ? min_col, min_row, max_col, max_row = merge_range.bounds
" w2 d* F' z" L6 s: k/ y print(min_row, min_col, max_row, max_col)
; o0 I; Y- V R& m print(source_start_row, source_start_col,source_end_row, source_end_col) I% B- ?" i# i7 }& \1 e' T
if (min_row >= source_start_row and max_row = source_start_col and max_col |
|