*convert docx file to epub*

def _docx_to_epub(
            self):
        """*convert docx file to epub*
        """
        self.log.debug('starting the ``_docx_to_epub`` method')

        if self.footer:
            footer = self._tmp_html_file(self.footer)
            footer = '"%(footer)s"' % locals()
        else:
            footer = ""

        if self.header:
            header = self._tmp_html_file(self.header)
            header = '"%(header)s"' % locals()
        else:
            header = ""

        # FIRST CONVERT THE DOC TO HTML
        docx = self.urlOrPath

        if self.title:
            title = self.title.replace(".html", "")
            html = "/tmp/" + self.title.replace(".html", "") + ".html"
        else:
            title = os.path.basename(docx).replace(
                ".docx", "").replace("_", " ")
            html = "/tmp/" + os.path.basename(docx).replace(".docx", ".html")
        pandoc = self.settings["executables"]["pandoc"]

        # TMP IMAGE DIR
        now = datetime.now()
        now = now.strftime("%Y%m%dt%H%M%S")
        imageDir = "/tmp/%(now)s" % locals()
        if not os.path.exists(imageDir):
            os.makedirs(imageDir)
        cmd = """%(pandoc)s --extract-media=%(imageDir)s -t html -f docx '%(docx)s' -o '%(html)s' """ % locals()
        p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
        stdout, stderr = p.communicate()
        self.log.debug('output: %(stdout)s' % locals())

        try:
            with open(html):
                pass
            fileExists = True
        except IOError:
            fileExists = False
            self.log.error(
                "the html %s does not exist on this machine, here is the failure message: %s" % (html, stderr))
            try:
                shutil.rmtree(imageDir)
            except:
                pass
            return None

        if fileExists:
            if self.outputDirectory:
                epub = self.outputDirectory + "/" + \
                    os.path.basename(html).replace(".html", ".epub")
            else:
                epub = docx.replace(".docx", ".epub")
            pandoc = self.settings["executables"]["pandoc"]

            cmd = """%(pandoc)s --metadata=title:'%(title)s' -S -s -f html -t epub3 %(header)s '%(html)s' %(footer)s -o '%(epub)s' """ % locals(
            )
            p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
            stdout, stderr = p.communicate()
            self.log.debug('output: %(stdout)s' % locals())

            try:
                shutil.rmtree(imageDir)
                os.remove(html)
            except:
                pass

            try:
                with open(epub):
                    pass
                fileExists = True
            except IOError:
                fileExists = False
                self.log.error(
                    "the epub %s does not exist on this machine, here is the failure message: %s" % (epub, stderr))
                return None

        self.log.debug('completed the ``_docx_to_epub`` method')
        return epub