【问题标题】:Using XLWings with parallel processing使用 XLWings 进行并行处理
【发布时间】:2017-10-05 14:40:38
【问题描述】:

所以我是并行处理的新手,但我开始让它同时解析多个 Excel 文件。当我只使用 openpyxl 时它工作得很好,但据我了解,这是一个基本的 XML 解析器。当我包含使用 XLWings 的部分时(我喜欢利用它在 Excel 中评估方程式以进行文件验证的能力),我收到以下错误:

pywintypes.com_error: (-2147221008, 'CoInitialize has not been called.', None, None)

这大致是我用来初始化新的 XLWings 实例并加载工作簿的代码:

def openWorkbook(self, filePath):
    app = xw.apps.add()
    app.display_alerts = False
    app.screen_updating = False
    wb = self.app.books(filePath) #Note that this is called only once for each workbook.
    app.screen_updating = True
    app.quit()

有没有办法让 XLWings 同时打开多个 Excel 实例?我应该尝试做类似this 的事情吗?如果是这样,我不确定初始化如何将线程交给 XLWings。

【问题讨论】:

    标签: python parallel-processing xlwings


    【解决方案1】:

    所以我想出了解决方案,它实际上非常简单。我刚刚在xw.apps.add() 调用之前从pythoncom 包中添加了pythoncom.CoInitialize()

    ParallelProcessController.py

    from multiprocessing.dummy import Pool
    from LoadWorkbook import openWorkbook
    
    def callOpenWorkbookInParallel(self, lsExcelFiles):
        pool = Pool(processes=3)
        
        pool.map(openWorkbook, lsExcelFiles)
    

    加载工作簿.py

    import xlwings as xw
    import pythoncom
    
    def openWorkbook(self, filePath):
      
      pythoncom.CoInitialize()
    
      app = xw.apps.add()
      wb = app.books(filePath)
      app.quit()
    

    【讨论】:

      猜你喜欢
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-24
      相关资源
      最近更新 更多