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

相关推荐
阿阳微客21 分钟前
Steam 搬砖项目深度拆解:从抵触到真香的转型之路
前端·笔记·学习·游戏
德育处主任Pro1 小时前
『React』Fragment的用法及简写形式
前端·javascript·react.js
CodeBlossom1 小时前
javaweb -html -CSS
前端·javascript·html
CodeCraft Studio1 小时前
【案例分享】如何借助JS UI组件库DHTMLX Suite构建高效物联网IIoT平台
javascript·物联网·ui
打小就很皮...2 小时前
HBuilder 发行Android(apk包)全流程指南
前端·javascript·微信小程序
集成显卡3 小时前
PlayWright | 初识微软出品的 WEB 应用自动化测试框架
前端·chrome·测试工具·microsoft·自动化·edge浏览器
前端小趴菜054 小时前
React - 组件通信
前端·react.js·前端框架
Amy_cx4 小时前
在表单输入框按回车页面刷新的问题
前端·elementui
沉到海底去吧Go4 小时前
【行驶证识别成表格】批量OCR行驶证识别与Excel自动化处理系统,行驶证扫描件和照片图片识别后保存为Excel表格,基于QT和华为ocr识别的实现教程
自动化·ocr·excel·行驶证识别·行驶证识别表格·批量行驶证读取表格
dancing9994 小时前
cocos3.X的oops框架oops-plugin-excel-to-json改进兼容多表单导出功能
前端·javascript·typescript·游戏程序