Search code examples
pythonpy2exewin32comtypelib

py2exe & win32 OLEObject error


I have tried to compile my program into an exe with py2exe. Unfortunately, as I am using win32com to copy charts from Excel and embed them into PowerPoint using (Shapes.PasteSpecial(ppPasteOLEObject), I constantly get this error:

File "win32com\client\__init__.pyc", line 170, in __getattr__
AttributeError: ppPasteOLEObject`.

Googling hasn't really helped. The script works perfectly when I run it in python, so I know the problem is with win32com. Using makepy.py to include typelibs also didn't help, but maybe my setup.pyis just wrong. So here it is:

import sys
from distutils.core import setup
import py2exe
from glob import glob
from os.path import normpath
import matplotlib

sys.setrecursionlimit(5000)
data_files=[("Microsoft.VC90.CRT",glob(normpath(
    r'C:/Program Files/Microsoft Visual Studio 9.0/VC/redist/x86/Microsoft.VC90.CRT/*.*'))),
    ("images",glob(normpath("images/*.PNG"))),
    ("ppttemplate",glob(normpath("ppttemplate/*.pptx")),
    (".",normpath("C:/windows/system32/ole32.dll")),
    (".",normpath("C:/Anaconda2/envs/py27/Library/bin/MSVCP90.dll")))
    ]
data_files.extend(matplotlib.get_py2exe_datafiles())

setup(
    data_files=data_files,
    console=['Main.py'],
    options={"py2exe":{"includes":["lxml.etree","lxml._elementpath","gzip",
                       "sip","PyQt4.QtGui","PyQt4.QtCore","matplotlib"],
                       "excludes":["Tkinter"],
                       "typelibs":[('{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}', 0, 2, 8),
                                   ('{00020430-0000-0000-C000-000000000046}', 0, 2, 0)]
                                   }
                                   }
                                   )

Thanks in advance for your help!


Solution

  • Got it! One just needs to inlude the relevant dlls and set skip_archive=True... (I included everything that py2exe was complaining about).

    By adding:

    sys.path.append("C:\\Program Files\\Microsoft VisualStudio9.0\\VC\\redist\\x86\\Microsoft.VC90.CRT")
    sys.path.append("C:\\windows\\system32")
    

    before the data_files line and adding all the system32 to data_files and adding skip_archive=True inside py2exe: it worked! Hopefully it functions on different computers as well...

    The guide I used was on the website of [py2exe] (http://www.py2exe.org/index.cgi/IncludingTypelibs)!