win32 复制多个 excel sheet 到 一个 新excel 中

在多线程里面使用win32com调用com组件的时候,需要用pythoncom.CoInitialize初始化一下。

最后还需要用pythoncom.CoUninitialize释放资源。

python 复制代码
import win32com.client as win32
import pythoncom

def copy_all_sheets_to_single_file(source_files, destination_file):
    pythoncom.CoInitialize()

    excel = win32.gencache.EnsureDispatch("Excel.Application")
    excel.Visible = False
    excel.DisplayAlerts = False

    try:
        destination_workbook = excel.Workbooks.Add()
        destination_sheet = destination_workbook.Worksheets(1)
        destination_sheet2 = destination_workbook.Worksheets(2)

        row_offset = 0
        for index, source_file in enumerate(source_files):
            source_workbook = excel.Workbooks.Open(source_file)
            source_sheet = source_workbook.Worksheets("Sheet1")
            source_sheet2 = source_workbook.Worksheets("Sheet2")

            source_range = source_sheet.UsedRange
            source_range2 = source_sheet2.UsedRange
            if index != 0:
                add_number = 6
            else:
                add_number = 0
            source_range.Copy(destination_sheet.Range("A" + str(row_offset + 1 + add_number)))
            source_range2.Copy(destination_sheet2.Range("A" + str(row_offset + 1 + add_number)))
            row_offset += source_range.Rows.Count

            source_workbook.Close(SaveChanges=False)  # 不保存更改,避免提示保存对话框

        destination_workbook.SaveAs(destination_file)
    except Exception as e:
        print(f"Error occurred: {str(e)}")
    finally:
        destination_workbook.Close()
        excel.Quit()

        excel.Application.Quit()
        win32.pythoncom.CoUninitialize()

# 使用示例
source_files = ["路径/到/源文件1.xlsx", "路径/到/源文件2.xlsx", ..., "路径/到/源文件10.xlsx"]
destination_file = "路径/到/目标文件.xlsx"
# 使用示例
soure_file = r"C:\Users\Administrator\Documents\Book1.xlsx"
soure_file2 = r"C:\Users\Administrator\Documents\Book2.xlsx"
source_files = [soure_file, soure_file2]
destination_file = r"C:\Users\Administrator\Documents\Book_new.xlsx"

copy_all_sheets_to_single_file(source_files, destination_file)


# 使用示例
soure_file = r"C:\Users\Administrator\Documents\Book1.xlsx"
soure_file2 = r"C:\Users\Administrator\Documents\Book2.xlsx"
source_files = [soure_file, soure_file]
destination_file = r"C:\Users\Administrator\Documents\Book_new.xlsx"

# copy_all_sheets_to_single_file(source_files, destination_file)

参考
https://yshblog.com/blog/57

相关推荐
technology_x几秒前
2026年财务报表分析软件测评:兼容与安全解析
java·服务器·前端
程序员鱼皮11 分钟前
Claude Opus 5 全新发布,7 大项目实测,夯还是拉?半价吊打 Fable 5?
前端·后端·ai编程
极简前端打杂工16 分钟前
从0到1搭建通用低代码平台(-)— 表单设计器
前端·全栈
吃饺子不吃馅17 分钟前
那就和前端好好道个别吧
前端
_瑞18 分钟前
试图教会你用 Xcode Instruments
前端·ios·xcode
海带紫菜菠萝汤1 小时前
WebCodecs API 实战:浏览器原生视频编解码的原理与性能测试
前端·javascript·音视频·视频编解码
一位正在转型AI全栈的前端工程师1 小时前
AI 全栈学习之旅 - Week2:从零搭建一个可部署的 AI 聊天应用
前端
aixingpan1 小时前
aixingpan.cn API开发文档:api_docs_trichart_natal_solararc_transit2接口指南
前端·php
Ayayoyo2 小时前
公平随机转盘的前端实现:Web Crypto API、拒绝采样与加权抽取
前端
程序员黑豆2 小时前
鸿蒙应用开发:@Link 装饰器实现父子组件双向同步
前端·后端·harmonyos