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

相关推荐
特立独行的猫A2 分钟前
仓颉语言实现的Harness: 一口气派三个子代理,cjh 的并发不是“多线程“,是“进程树“
前端·harmonyos
ms365copilot3 分钟前
Copilot 代理开始进入Word、Excel、PowerPoint,不再只是“问一句答一句”
word·excel·copilot
Dumbness4 分钟前
农历转换的 4 个边界:2 月 30 日能转出"正月十四"、小月没有三十、闰月、立春到春节之间的干支
前端·javascript
不可能片场12 分钟前
工程实践 日志文件名是北京日期 内容却是 UTC
前端·electron
系统集成-鹰翔14 分钟前
ERP接口月度统计少了零单月份:SQLite分组与最近三个月均值排查
javascript·数据库·经验分享·后端
徐小夕18 分钟前
JitWord 4.3 发布!Word 内嵌 Visio 图表、原生流程图一键无损解析
前端·vue.js·github
挖掘狂人18 分钟前
全栈正在杀死前端?被淘汰的从来不是岗位,只会是执行层
前端·ai编程·全栈
sycmancia20 分钟前
Qml——Button元素
前端
codigger24 分钟前
前端没有被杀死,被杀死的是"只会把设计稿变成页面"
前端·ai·ai编程·编程语言·全栈·#人工智能
艺杯羹28 分钟前
本地优先架构避坑:从重度云端依赖到客户端算力下沉与边缘自治实战
前端·系统架构·本地优先