自从office2007版开始,微软的office套件就启用了新的格式,其中powerpoint的格式由ppt升为了pptx,这两种格式是完全不一样的。到目前为止,还有很多文档是ppt,但变到pptx是迟早的事,在此我编制了一个小工具,实现从ppt转到pptx,不过工具并不是解析ppt然后转换的,所以速度也不会太快,但对于日常使用应是能完全满足的了。


# coding = gbkimport osfrom win32com import clientclass ppt2pptx(): """ppt转pptx""" def __init__(self,params): self.errFlag = False self.msg = "" self.sourcePath = params["sourcePath"] self.targetPath = params["targetPath"] self.errorPath = params["errorPath"] self.deleteFlag = params["deleteFlag"] if not os.path.exists(self.sourcePath): self.msg = "源目录有误,请检查!{}".format(self.sourcePath) self.errFlag = True if not os.path.exists(self.targetPath): os.makedirs(self.targetPath) if not os.path.exists(self.errorPath): os.makedirs(self.errorPath) def run(self): if self.errFlag == True: print(self.msg) return pptObj = client.Dispatch('PowerPoint.Application') for file in os.listdir(self.sourcePath): # 得到文件名与扩展名 fileName,expadName = os.path.splitext(file) if expadName.upper() != ".PPT": continue try: prs = pptObj.Presentations.Open(os.path.join(self.sourcePath,file),Untitled=0,WithWindow=0) prs.SaveAs(os.path.join(self.targetPath,fileName + ".pptx")) print("已转换:{}".format(file)) if self.deleteFlag: os.remove(self.sourcePath,file) except: print("转换失败:{}".format(file)) try: os.rename(os.path.join(self.sourcePath,file),os.path.join(self.targetPath,file)) except: continue continueif __name__ == '__main__': params = { "sourcePath":r"K:伍德春原创视频自动化2020-11-10", "targetPath":r"K:伍德春原创视频自动化2020-11-10pptx", "errorPath":r"K:伍德春原创视频自动化2020-11-10error", # 文件出错时放置的目录 "deleteFlag":False, # 是否删除源文件,True-删除,Flase-不删除 } newobj = ppt2pptx(params) newobj.run()