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

相关推荐
明月_清风6 分钟前
前端工程化七连问:从紧急修复到版本控制,一文打通工程化任督二脉
前端·前端工程化
用户67570498850210 分钟前
不装插件不写代码!教你一招搞定网页长截图!清晰且高效!
前端·chrome
tjl521314_2114 分钟前
01C++ 分离编译与多文件编程
前端·c++·算法
sayamber17 分钟前
vLLM 容器化部署实战:如何在云服务器上跑起高并发大模型推理服务
前端
LIO17 分钟前
Pinia 极简指南:Vue 3 官方状态管理库
前端·vue.js
Wenzar_1 小时前
# D3.js实战进阶:从基础图表到交互式数据仪表盘的全流程构建在现代前端开发中,**数据可视化已成为提升用户体验的核心能力之一
java·javascript·python·信息可视化·ux
燐妤1 小时前
前端HTML编程2:深入学习表单与表格
前端·学习·html5
菜鸟小码1 小时前
MapReduce 编程模型详解:Mapper、Reducer、Driver 三大核心组件
大数据·javascript·mapreduce
朝阳391 小时前
react【实战】首页 -- 响应式导航栏(含带联动动画的搜索框)
前端·react.js·前端框架
贾铭1 小时前
如何实现一个网页版的剪映(五)如何跳转到视频某一帧
前端·后端