合并两个Excel文档
代码的主要功能是合并两个Excel文档:第一个文档如下:
![图一](https://pic.imgdb.cn/item/6715be5fd29ded1a8c567034.png)
第二个文档如下:
![](https://pic.imgdb.cn/item/6715be5fd29ded1a8c56707e.png)
想要合并成下面图片的样子:
![](https://pic.imgdb.cn/item/6715be5fd29ded1a8c5670b6.png)
下面代码实现:
```python
"""
Date:2024/10/21 15:46
File : MergeExcel.py
"""
from copy import copy
from openpyxl import load_workbook
class MergeExcel:
def __init__(self):
e1 = "fileDirectory/excel1.xlsx"
e2 = "fileDirectory/excel2.xlsx"
self.wb1 = load_workbook(e1)
self.wb2 = load_workbook(e2)
self.sheet1 = self.wb1.active
self.sheet2 = self.wb2.active
@staticmethod
def copy_style(source_cell, target_cell):
if source_cell.has_style:
target_cell.font = copy(source_cell.font)
target_cell.fill = copy(source_cell.fill)
target_cell.border = copy(source_cell.border)
target_cell.alignment = copy(source_cell.alignment)
target_cell.number_format = source_cell.number_format
target_cell.protection = copy(source_cell.protection)
def apply_merge_cell(self, merged_ranges):
for min_row, min_col, max_row, max_col in merged_ranges:
self.sheet2.merge_cells(start_row=min_row, start_column=min_col,
end_row=max_row, end_column=max_col)
def copy_value_style(self, source_start_row, source_end_row, source_start_col, source_end_col, target_start_row,
target_start_col):
for row in range(source_start_row, source_end_row + 1):
for col in range(source_start_col, source_end_col + 1):
source_cell = self.sheet1.cell(row=row, column=col)
target_cell = self.sheet2.cell(row=row + target_start_row - source_start_row,
column=col + target_start_col - source_start_col)
self.sheet2.cell(row=row + target_start_row - source_start_row,
column=col + target_start_col - source_start_col).value = source_cell.value
self.copy_style(source_cell, target_cell)
def copy_stats(self, source_start_row, source_end_row, source_start_col, source_end_col, target_start_row,
target_start_col):
merged_ranges = []
for merge_range in self.sheet1.merged_cells.ranges:
min_col, min_row, max_col, max_row = merge_range.bounds
if (min_row >= source_start_row and max_row <= source_end_row and
min_col >= source_start_col and max_col <= source_end_col):
merged_ranges.append((min_row + target_start_row - source_start_row,
min_col + target_start_col - source_start_col,
max_row + target_start_row - source_start_row,
max_col + target_start_col - source_start_col))
self.apply_merge_cell(merged_ranges)
def main(self):
self.copy_value_style(1, self.sheet1.max_row, 1, self.sheet1.max_column, self.sheet1.max_row + 2, 1)
self.copy_stats(1, self.sheet1.max_row, 1, self.sheet1.max_column, self.sheet1.max_row + 2, 1)
self.wb2.save('fileDirectory/MergeExcel.xlsx')
print("Done...")
if __name__ == '__main__':
merge_excel = MergeExcel()
merge_excel.main()
```
简单的代码,希望能帮助到像我一样的小白,大佬勿喷。
如果有不妥之处希望您不吝赐教。 编程不会,下来看看 我之前用vba写了一个,可以实现一个excel文件内多个sheet表中的内容顺次复制到第一个sheet中,并且删除后面的sheet,还能跳过空sheet,只需要十几行代码。 很实用的技巧,简单易懂 你的这个,用VBA特别特别特别简单。。。你可以试试 感谢分享
日常办公用处大
学习了 学习到了,可以的 这个代码需要在那个环境合并 学习了 谢谢大佬 虽然不太懂代码,但支持一下 这个代码不错 学习了