python-win32com excel com模型开始生成错误 [英] python-win32com excel com model started generating errors

查看:247
本文介绍了python-win32com excel com模型开始生成错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几天里,我一直在为一些报表自动生成一些数据透视表.

Over the last few days, I have been working on automating the generation of some pivot tables for a number of reports.

简而言之,以下代码可以正常工作:

Boiled down to the minimum, the following code was working without issue:

import win32com.client    
objExcelApp = win32com.client.gencache.EnsureDispatch('Excel.Application')
objExcelApp.Visible = 1

这将弹出一个excel实例,我可以继续在Python中工作.但是突然之间,今天我的脚本因以下原因而失败:

This would pop-up an instance of excel and I could continue working in Python. But suddenly, today my scripts are failing with the following:

>>>import win32com.client
>>> objExcelApp = win32com.client.gencache.EnsureDispatch('Excel.Application')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\win32com\client\gencache.py", line 534, in EnsureDispatch
    mod = EnsureModule(tla[0], tla[1], tla[3], tla[4], bForDemand=bForDemand)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\win32com\client\gencache.py", line 391, in EnsureModule
    module = GetModuleForTypelib(typelibCLSID, lcid, major, minor)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\win32com\client\gencache.py", line 266, in GetModuleForTypelib
    AddModuleToCache(typelibCLSID, lcid, major, minor)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\win32com\client\gencache.py", line 552, in AddModuleToCache
    dict = mod.CLSIDToClassMap
AttributeError: module 'win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x9' has no attribute 'CLSIDToClassMap'

代码从昨天更改为今天.我不知道发生了什么!!!

The code has not changed from yesterday to today. I have no idea what is happening!!!.

另一个有趣的踢脚者.如果我再次在同一会话中执行相同的代码,则会收到不同的错误:

Another interesting kicker. if I do the same code in the same session again I get a different error:

>>> objExcelApp = win32com.client.gencache.EnsureDispatch('Excel.Application')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\win32com\client\gencache.py", line 534, in EnsureDispatch
    mod = EnsureModule(tla[0], tla[1], tla[3], tla[4], bForDemand=bForDemand)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\win32com\client\gencache.py", line 447, in EnsureModule
    if module.MinorVersion != tlbAttributes[4] or genpy.makepy_version != module.makepy_version:
AttributeError: module 'win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x9' has no attribute 'MinorVersion'
>>>

因此,我跳到安装了全新Windows的Windows机器上,安装python37和pip install pypiwin32.运行相同的行,并像昨天在我的原始计算机上一样打开excel.

So I jump over to a windows machine with a fresh windows install, install python37 and pip install pypiwin32. Run the very same lines and excel opens just like it did yesterday on my original machine.

我尝试卸载并重新安装,但是没有运气.知道这里发生了什么吗?

I tried un-installing and re-installing with no luck. Any idea what is going on here?

注意:动态调度仍然有效:

NOTE: Dynamic dispatch still works:

import win32com.client
objExcelApp = win32com.client.Dispatch("Excel.Application")
objExcelApp.Visible = 1

但是我特别需要静态分配,因为数据透视表不能与动态分配的对象一起使用(在我的代码的后面):

But I specifically need static dispatch as Pivot Tables won't work with a dynamically dispatched object (much later in my code):

objExcelPivotCache = objExcelWorkbook.PivotCaches().Create(SourceType=win32c.xlDatabase, SourceData=objExcelPivotSourceRange)

推荐答案

我遇到了同样的问题,并且按照以下说明进行了解决:

I had the same issue and I resolved it by following the instructions here: https://mail.python.org/pipermail/python-win32/2007-August/006147.html

删除gen_py输出目录并重新运行makepy SUCCEEDS然后测试应用程序再次运行正常.

Deleting the gen_py output directory and re-running makepy SUCCEEDS and subsequently the test application runs OK again.

因此症状已解决,但是有关如何解决的任何线索发生了这是一个非常长时间运行的应用程序(请考虑使用24x7年),我担心无论是什么原因导致这种情况再次发生.

So the symptom is resolved, but any clues as to how this could have happened. This is a VERY long running application (think 24x7 for years) and I'm concerned that whatever caused this might occur again.

要找到输出目录,请在您的python控制台/python会话中运行它:

To find the output directory, run this in your python console / python session:

import win32com
print(win32com.__gen_path__)

根据您帖子中的异常消息,您需要删除的目录将被命名为"00020813-0000-0000-C000-000000000046x0x1x9".因此,删除该目录并重新运行代码.而且,如果您担心要删除它(就像我以前一样),只需剪切目录并将其粘贴到其他位置即可.

Based on the exception message in your post, the directory you need to remove will be titled '00020813-0000-0000-C000-000000000046x0x1x9'. So delete this directory and re-run the code. And if you're nervous about deleting it (like I was) just cut the directory and paste it somewhere else.

我不知道为什么会发生这种情况,也不知道如何防止这种情况再次发生,但是我提供的链接中的指示似乎对我有用.

I have no idea why this happens nor do I know how to prevent it from happening again, but the directions in the link I provided seemed to work for me.

这篇关于python-win32com excel com模型开始生成错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆