1
+ '''
2
+ @jiangmiemie(jiangyangcreate@gmail)
3
+ 自动分辨输入文件路径或文件夹路径,转换指定后缀文件为PDF('doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx')
4
+ '''
5
+
6
+ import os
7
+ from pathlib import Path
8
+ from win32com .client import Dispatch , gencache , DispatchEx
9
+ import win32com .client
10
+
11
+ class PDFConverter :
12
+ def __init__ (self , pathname ):
13
+ self ._handle_postfix = ['doc' , 'docx' , 'ppt' , 'pptx' , 'xls' , 'xlsx' ]
14
+ self ._filename_list = list ()
15
+ self ._export_folder = os .path .join (os .path .abspath ('.' ), outpath )
16
+ if not os .path .exists (self ._export_folder ):
17
+ os .mkdir (self ._export_folder )
18
+ self ._enumerate_filename (pathname )
19
+
20
+ def _enumerate_filename (self , pathname ):
21
+ full_pathname = os .path .abspath (pathname )
22
+ if os .path .isfile (full_pathname ):
23
+ if self ._is_legal_postfix (full_pathname ):
24
+ self ._filename_list .append (full_pathname )
25
+ else :
26
+ raise TypeError ('文件 {} 后缀名不合法!仅支持如下文件类型:{}。' .format (
27
+ pathname , '、' .join (self ._handle_postfix )))
28
+ elif os .path .isdir (full_pathname ):
29
+ for relpath , _ , files in os .walk (full_pathname ):
30
+ for name in files :
31
+ filename = os .path .join (full_pathname , relpath , name )
32
+ if self ._is_legal_postfix (filename ):
33
+ self ._filename_list .append (os .path .join (filename ))
34
+ else :
35
+ raise TypeError ('文件/文件夹 {} 不存在或不合法!' .format (pathname ))
36
+
37
+ def _is_legal_postfix (self , filename ):
38
+ return filename .split ('.' )[- 1 ].lower () in self ._handle_postfix and not os .path .basename (filename ).startswith ('~' )
39
+
40
+ def run_conver (self ):
41
+ '''
42
+ 进行批量处理,根据后缀名调用函数执行转换
43
+ '''
44
+ print ('需要转换的文件数:' , len (self ._filename_list ))
45
+ for filename in self ._filename_list :
46
+ postfix = filename .split ('.' )[- 1 ].lower ()
47
+ funcCall = getattr (self , postfix )
48
+ print ('原文件:' , filename )
49
+ funcCall (filename )
50
+ print ('转换完成!' )
51
+
52
+ def doc (self , filename ):
53
+ '''
54
+ doc 和 docx 文件转换
55
+ '''
56
+ name = os .path .basename (filename ).split ('.' )[0 ] + '.pdf'
57
+ word = Dispatch ('Word.Application' )
58
+ doc = word .Documents .Open (filename )
59
+ pdf_file = os .path .join (self ._export_folder , name )
60
+ doc .SaveAs (pdf_file , FileFormat = 17 )
61
+ doc .Close ()
62
+ word .Quit ()
63
+
64
+ def docx (self , filename ):
65
+ self .doc (filename )
66
+
67
+ def xls (self , filename ):
68
+ '''
69
+ xls 和 xlsx 文件转换
70
+ '''
71
+ name = os .path .basename (filename ).split ('.' )[0 ] + '.pdf'
72
+ exportfile = os .path .join (self ._export_folder , name )
73
+ xlApp = DispatchEx ("Excel.Application" )
74
+ xlApp .Visible = False
75
+ xlApp .DisplayAlerts = 0
76
+ books = xlApp .Workbooks .Open (filename , False )
77
+ books .ExportAsFixedFormat (0 , exportfile )
78
+ books .Close (False )
79
+ print ('保存 PDF 文件:' , exportfile )
80
+ xlApp .Quit ()
81
+
82
+ def xlsx (self , filename ):
83
+ self .xls (filename )
84
+
85
+ def ppt (self ,filename ):
86
+ """
87
+ PPT文件导出为pdf格式
88
+ :param filename: PPT文件的名称
89
+ :param output_filename: 导出的pdf文件的名称
90
+ :return:
91
+ """
92
+ name = os .path .basename (filename ).split ('.' )[0 ] + '.pdf'
93
+ exportfile = os .path .join (self ._export_folder , name )
94
+ ppt_app = win32com .client .Dispatch ('PowerPoint.Application' )
95
+ ppt = ppt_app .Presentations .Open (filename )
96
+ ppt .SaveAs (exportfile , 32 )
97
+ print ('保存 PDF 文件:' , exportfile )
98
+ ppt_app .Quit ()
99
+
100
+ def pptx (self , filename ):
101
+ self .ppt (filename )
102
+
103
+
104
+ def main (In_Path ):
105
+ my_file = Path (In_Path )
106
+ if my_file .is_dir (): # 判断是否为文件夹
107
+ pathname = os .path .join (os .path .abspath ('.' ), In_Path )
108
+ else :
109
+ pathname = In_Path # 单个文件的转换
110
+ pdfConverter = PDFConverter (pathname )
111
+ pdfConverter .run_conver ()
112
+
113
+ if __name__ == "__main__" :
114
+ outpath = 'SavePath'
115
+ main (input ('输入你要转化的文件或文件夹路径' ))
0 commit comments