コード例 #1
0
ファイル: com_run.py プロジェクト: yuhisern7/macro_pack
    def run(self):
        # Extract information

        logging.info(" [+] Running %s on local PC..." % self.comTarget)
        if not os.path.isfile(self.comTarget):
            logging.error("   [!] Could not find %s " % self.comTarget)
            return

        targetApp = MSTypes.guessApplicationType(self.comTarget)
        if MSTypes.XL in targetApp:
            comApp = "Excel.Application"
        elif MSTypes.WD in targetApp:
            comApp = "Word.Application"
        elif MSTypes.PPT in targetApp:
            comApp = "PowerPoint.Application"
        elif MSTypes.VSD in targetApp:
            comApp = "Visio.InvisibleApp"
        elif MSTypes.ACC in targetApp:
            comApp = "Access.Application"
        elif MSTypes.MPP in targetApp:
            comApp = "MSProject.Application"
        else:
            logging.error("   [!] Could not recognize file extension for %s" %
                          self.comTarget)
            return

        try:
            logging.info("   [-] Create handler...")
            comObj = win32com.client.Dispatch(comApp)
        except:
            logging.exception("   [!] Cannot access COM!")
            return

        # We need to force run macro if it is not triggered at document open
        if self.startFunction and self.startFunction not in self.potentialStartFunctions:
            logging.info("   [-] Run macro %s..." % self.startFunction)
        else:
            logging.info(
                "   [-] No specific start function, running auto open macro..."
            )

        # do the operation in background without actually opening Excel
        try:
            if MSTypes.XL in targetApp:
                document = comObj.Workbooks.Open(self.comTarget)
            elif MSTypes.WD in targetApp or MSTypes.VSD in targetApp:
                document = comObj.Documents.Open(self.comTarget)
            elif MSTypes.PPT in targetApp:
                document = comObj.Presentations.Open(self.comTarget)
            elif MSTypes.ACC in targetApp:
                document = comObj.OpenCurrentDatabase(self.comTarget)
            elif MSTypes.MPP in targetApp:
                document = comObj.FileOpen(self.comTarget, True)
            if self.startFunction and self.startFunction not in self.potentialStartFunctions:
                document = comObj.run(self.startFunction)
        except Exception:
            logging.exception("   [!] Problem detected!")

        logging.info("   [-] Cleanup...")
        try:
            document.close()
            comObj.Application.Quit()
        except:
            pass
        try:
            comObj.FileClose()
        except:
            pass
        try:
            comObj.Quit()
        except:
            pass
        # garbage collection
        del comObj

        logging.info("   [-] OK!")
コード例 #2
0
 def outputFilePath(self, outputFilePath):
     self._outputFilePath = outputFilePath
     self._outputFileType = MSTypes.guessApplicationType(
         self._outputFilePath)
コード例 #3
0
 def inputFilePath(self, inputFilePath):
     self._inputFilePath = inputFilePath
     self._inputFileType = MSTypes.guessApplicationType(self._inputFilePath)