try:
xl = client.gencache.EnsureDispatch('Excel.Application')
except AttributeError:
# Corner case dependencies.
import os
import re
import sys
import shutil
# Remove cache and try again.
MODULE_LIST = [m.__name__ for m in sys.modules.values()]
for module in MODULE_LIST:
if re.match(r'win32com\.gen_py\..+', module):
del sys.modules[module]
shutil.rmtree(os.path.join(os.environ.get('LOCALAPPDATA'), 'Temp', 'gen_py'))
from win32com import client
xl = client.gencache.EnsureDispatch('Excel.Application')
参考
https://gist.github.com/rdapaz/63590adb94a46039ca4a10994dff9dbe