找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 70|回复: 0

python合并两个Excel文件的脚本

[复制链接]

2万

主题

162

回帖

18万

积分

管理员

积分
184652
发表于 2024-10-22 22:25:46 | 显示全部楼层 |阅读模式 IP:山东省济南市 联通

登录后更精彩...O(∩_∩)O...

您需要 登录 才可以下载或查看,没有账号?立即注册

×
python合并两个Excel文件的脚本



合并两个Excel文档:
第一个文档如下:
第二个文档如下:
想要合并成下面图片的样子:
学习一段时间了,写了个python合并两个Excel文件的脚本,希望能帮助到像我一样的小白,代码简单,大佬勿喷。不足之处请您不吝赐教:
下面代码实现:




[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
        self.insert_row = 99             # 这里设置从哪行插入第一张表的数据 或者 self.sheet2.max_row + 1

    @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)
            # 使用openpyxl库的cell.comment属性获取单元格的注释。
            if source_cell.comment:
                target_cell.comment = source_cell.comment  # 有批注的复制批注

    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
            print(min_row, min_col, max_row, max_col)
            print(source_start_row, source_start_col,source_end_row, source_end_col)
            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_end_row - 1,
                                      min_col + target_start_col - source_start_col,
                                      max_row + target_start_row - source_end_row - 1,
                                      max_col + target_start_col - source_start_col))
                self.apply_merge_cell(merged_ranges)
        print(merged_ranges)

    def main(self):
        self.copy_value_style(1, self.sheet1.max_row, 1, self.sheet1.max_column, self.insert_row, 1)
        self.copy_stats(1, self.sheet1.max_row, 1, self.sheet1.max_column, self.sheet2.max_row + 1, 1)

        # 保存工作簿
        self.wb2.save('fileDirectory/MergeExcel.xlsx')
        print("Done...")


if __name__ == '__main__':
    merge_excel = MergeExcel()
    merge_excel.main()





回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|哩布大模型|Civitai大模型|IP定位|图反推|站长素材|deepseek|即梦视频|阿狗工具|花瓣网|pinterest|php手册|宝塔文档|CyberChef|猫捉鱼铃|手机版|小黑屋|下载狗|IPS|在线工具|分享屋 ( 鲁ICP备2021028754号 )

GMT+8, 2025-5-4 10:48

Powered by 分享屋 X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表