本文實例為大家分享了python3將docx轉換成pdf文件的具體代碼,供大家參考,具體內容如下
直接上代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# -*- encoding:utf-8 -*- """ author:lgh """ from win32com.client import Dispatch, constants, gencache 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 True except : return False finally : w.Quit(constants.wdDoNotSaveChanges) def GenerateSupport(): gencache.EnsureModule( '{00020905-0000-0000-C000-000000000046}' , 0 , 8 , 4 ) def main(): input = r 'xxx\xxx.docx' output = r 'xxx\xxx.pdf' # GenerateSupport() rc = doc2pdf( input , output) if rc: print ( '轉換成功' ) else : print ( '轉換失敗' ) if __name__ = = '__main__' : main() |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/lgh344902118/p/8629238.html