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

相关推荐
十有八七12 小时前
手搓 Agent(一):先有大脑——大模型只会做一件事
前端·人工智能·后端
锋行天下12 小时前
「踩坑实录」给 LangGraph Agent 接两个 FastMCP,我被 langchain 偷偷改了工具名
前端·人工智能·后端
跳跳的小星星12 小时前
自研组件】Vue2 + ElementUI 通用列表页封装 wkjp-list-page,一套配置搞定搜索、动态表格、分页、增删改
前端
tryCbest12 小时前
Vue3—— 第 18 篇:Vue 2 到 Vue 3 迁移指南——升级避坑全记录
前端·vue.js
保利威视频SaaS小张13 小时前
企业直播接入笔记 12|企业内训直播怎么做签到和学时统计,防挂机这件事比想象中难
java·前端·数据库·笔记·视频云·polvy·polyv
bysking13 小时前
GitHub Actions 快速入门+场景实践案例
前端·后端·github
广州华水科技14 小时前
单北斗GNSS变形监测系统在大坝安全维护中的重要性
前端
anyup14 小时前
自带 uni-app x 页面级能力,uView Pro X 8 个开箱即用的工具函数
前端·vue.js·uni-app
涛涛ing14 小时前
别拿 Chrome 跑通了就提测:把这段 AI 写的代码发到 Safari 上,我差点丢了工作
前端
墨林陌14 小时前
用 AI 给娃做了一个识物游戏:一份 2.3MB 的单文件 HTML,126 张卡片、三种玩法,手机平板打开就能玩
前端