Python實現(xiàn)將DOC文檔轉換為PDF的方法
更新時間:2015年07月25日 10:15:05 作者:Sephiroth
這篇文章主要介紹了Python實現(xiàn)將DOC文檔轉換為PDF的方法,涉及Python調用系統(tǒng)win32com組件實現(xiàn)文件格式轉換的相關技巧,需要的朋友可以參考下
本文實例講述了Python實現(xiàn)將DOC文檔轉換為PDF的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
import sys, os from win32com.client import Dispatch, constants, gencache def usage(): sys.stderr.write ("doc2pdf.py input [output]") sys.exit(2) def doc2pdf(input, output): w = Dispatch("Word.Application") try: doc = w.Documents.Open(input, ReadOnly = 1) doc.ExportAsFixedFormat(output, constants.wdExportFormatPDF, Item = constants.wdExportDocumentWithMarkup, CreateBookmarks = constants.wdExportCreateHeadingBookmarks) return 0 except: return 1 finally: w.Quit(constants.wdDoNotSaveChanges) # Generate all the support we can. def GenerateSupport(): # enable python COM support for Word 2007 # this is generated by: makepy.py -i "Microsoft Word 12.0 Object Library" gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4) def main(): if (len(sys.argv) == 2): input = sys.argv[1] output = os.path.splitext(input)[0]+'.pdf' elif (len(sys.argv) == 3): input = sys.argv[1] output = sys.argv[2] else: usage() if (not os.path.isabs(input)): input = os.path.abspath(input) if (not os.path.isabs(output)): output = os.path.abspath(output) try: GenerateSupport() rc = doc2pdf(input, output) return rc except: return -1 if __name__=='__main__': rc = main() if rc: sys.exit(rc) sys.exit(0)
希望本文所述對大家的Python程序設計有所幫助。
相關文章
TensorFLow 不同大小圖片的TFrecords存取實例
今天小編就為大家分享一篇TensorFLow 不同大小圖片的TFrecords存取實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01手把手教你將Flask應用封裝成Docker服務的實現(xiàn)
這篇文章主要介紹了手把手教你將Flask應用封裝成Docker服務,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08python對輸出的奇數(shù)偶數(shù)排序實例代碼
在本篇內容里小編給大家整理的是一篇關于python對輸出的奇數(shù)偶數(shù)排序實例代碼內容,有興趣的朋友們可以參考下。2020-12-12Python Web框架Flask信號機制(signals)介紹
這篇文章主要介紹了Python Web框架Flask信號機制(signals)介紹,本文介紹Flask的信號機制,講述信號的用途,并給出創(chuàng)建信號、訂閱信號、發(fā)送信號的方法,需要的朋友可以參考下2015-01-01