在平时的工作中,难免需要一些小Tips来解决工作中遇到的问题。今天的文章将为您提供方便快捷的技巧,将Office(doc/docx/ppt/pptx/xls/xlsx)文件批量或单个文件转换为PDF文件。不过在进行具体操作之前,需要先在PC上安装Office,然后使用Python的win32com包来实现Office文件的转换。在安装win32com之前,需要先安装Pythonwin32com。详细安装步骤如下:使用pip命令安装pipinstallpywin32。如果我们遇到安装错误,可以通过python-mpipinstall--upgradepip更新云,然后安装:python-mpipinstall--upgradepip下载离线安装包安装如果pip命令没有安装成功,可以同样下载离线包安装,方法步骤如下:首先在官网选择对应的Python版本下载离线包:https://sourceforge.net/projects/pywin32/files/pywin32/Build%20221/下载后,像傻瓜一样安装。文件转换逻辑的详细代码如下:classPDFConverter:def__init__(self,pathname,export='.'):self._handle_postfix=['doc','docx','ppt','pptx','xls','xlsx']#支持转换的文件类型self._filename_list=list()#列出文件self._export_folder=os.path.join(os.path.abspath('.'),'file_server/pdfconver')ifnotos.path.exists(self._export_folder):os.mkdir(self._export_folder)self._enumerate_filename(pathname)def_enumerate_filename(self,pathname):'''读取所有文件名'''full_pathname=os.path.abspath(pathname)ifos。path.isfile(full_pathname):ifself._is_legal_postfix(full_pathname):self._filename_list.append(full_pathname)else:raiseTypeError('文件{}的后缀不合法!只支持以下文件类型:{}。'.format(pathname,','.join(self._handle_postfix)))elifos.path.isdir(full_pathname):forrelpath,_,filesinos.walk(full_pathname):fornameinfiles:filename=os.path.join(full_pathname,relpath,name)ifself._is_legal_postfix(filename):self._filename_list.append(os.path.join(filename))else:raiseTypeError('文件/文件夹{}不存在或不合法!'.format(pathname))def_is_legal_postfix(self,filename):returnfilename.split('.')[-1].lower()inself._handle_postfixandnotos.path.basename(filename).startswith('~')defrun_conver(self):print('需要转换的文件个数为:',len(self._filename_list))forfilenameinself._filename_list:postfix=filename.split('.')[-1].lower()funcCall=getattr(self,postfix)print('原始文件:',filename)funcCall(filename)print('Conversioncompleted!')doc/docx转PDFdoc/docx转PDF部分代码如下:defdoc(self,filename):name=os.path.basename(文件名).split('.')[0]+'.pdf'exportfile=os.path.join(self._export_文件夹,名称)打印('保存PDF文件:',导出文件)gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}',0,8,4)pythononcom.CoInitialize()w=Dispatch("Word.Application")pythoncom.CoInitialize()#添加防止CoInitialize加载doc=w.Documents.Open(filename)doc.ExportAsFixedFormat(exportfile,constants.wdExportFormatPDF,Item=constants.wdExportDocumentWithMarkup,CreateBookmarks=constants.wdExportCreateHeadingBookmarks)w.Quit(constants.wdDoNotSaveChanges)defdocx(self,filename):self.doc(filename)ppt/pptxtoPDFppt/pptxtoPDF部分代码如下:defppt(self,filename)):name=os.path.basename(filename).split('.')[0]+'.pdf'exportfile=os.path.join(self._export_folder,name)gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}',0,8,4)pythoncom.CoInitialize()p=Dispatch("PowerPoint.Application")pythoncom.CoInitialize()ppt=p.Presentations.Open(文件名,False,False,False)ppt.ExportAsFixedFormat(exportfile,2,PrintRange=None)print('保存PDF文件:',exportfile)p.Quit()defpptx(self,filename):self.ppt(filename)xls/xlsx转换为PDFdefxls(self,filename):name=os.path.basename(文件名)。分裂('。')[0]+'.pdf'exportfile=os.path.join(self._export_folder,name)pythoncom.CoInitialize()xlApp=DispatchEx("Excel.Application")pythoncom.CoInitialize()xlApp.Visible=FalsexlApp.DisplayAlerts=0books=xlApp.Workbooks.Open(filename,False)books.ExportAsFixedFormat(0,exportfile)books.Close(False)print('保存PDF文件:',exportfile)xlApp.Quit()defxlsx(self,filename):self.xls(filename)执行转换逻辑if__name__=="__main__":#支持文件夹批量导入#folder='tmp'#pathname=os.path.join(os.path.abspath('.'),folder)#还支持单个文件pathname="G:/python_study/test.doc"pdfConverter=PDFConverter(pathname)pdfConverter.run_conver()总结今天的文章主要是Python实战小工具的使用,希望对大家有所帮助对大家有帮助
