-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathword2pdf.py
62 lines (55 loc) · 2.22 KB
/
word2pdf.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding:utf-8 -*-
import os
from win32com.client import Dispatch, DispatchEx
from win32com.client import constants
from win32com.client import gencache
# Word to PDF
class Word2PDF:
def __init__(self):
self.infn = []
self.message = ''
def run(self):
valueList = []
try:
self.message = '开始转换, 请稍后'
try:
gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4)
w = Dispatch("Word.Application")
except:
self.message = '电脑上可能没有安装Office, 无法使用'
return
for i in range(len(self.infn)):
filename = self.infn[i]
(filepath, filename) = os.path.split(filename)
softfilename = os.path.splitext(filename)
os.chdir(filepath)
doc = os.path.abspath(filename)
os.chdir(filepath)
pdfname = softfilename[0] + ".pdf"
output = os.path.abspath(pdfname)
pdf_name = output
try:
doc = w.Documents.Open(doc, ReadOnly=1)
doc.ExportAsFixedFormat(output, constants.wdExportFormatPDF,
Item=constants.wdExportDocumentWithMarkup,
CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
self.message = str(i+1) + '/' + str(len(self.infn))
except:
self.message = '出错了(1): ' + filename + ' 转换失败'
return
if os.path.isfile(pdf_name):
valueList.append(pdf_name)
else:
self.message = '出错了(2): ' + filename + ' 转换失败'
return
w.Quit(constants.wdDoNotSaveChanges)
self.message = '完成'
return valueList
except:
self.message = '出错了, 已停止转换'
return
if __name__=='__main__':
w2p = Word2PDF()
w2p.infn = ['F:1.docx', 'F:2.doc']
w2p.run()
print(w2p.message)