Skip to content

Instantly share code, notes, and snippets.

@rdapaz
Created March 12, 2022 00:27
Show Gist options
  • Save rdapaz/48cd3d24096d8832d6bcf038eec02283 to your computer and use it in GitHub Desktop.
Save rdapaz/48cd3d24096d8832d6bcf038eec02283 to your computer and use it in GitHub Desktop.
Fix pycom errors
def GetWin32comObject(object_name='Word.Application'):
from win32com import client
try:
object = client.gencache.EnsureDispatch(object_name)
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
object = client.gencache.EnsureDispatch(object_name)
return object
# then later...
class Word:
def __init__(self):
self.app = GetWin32comObject("Word.Application")
self.app.Visible = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment