Created
March 12, 2022 00:27
-
-
Save rdapaz/48cd3d24096d8832d6bcf038eec02283 to your computer and use it in GitHub Desktop.
Fix pycom errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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