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
	
from win32com.client import Dispatch
import os
from time import sleep

wdFormatPDF = 17

def doc2pdf(input_file,output_file):
print(input_file)
print(output_file)
word = Dispatch('Word.Application')
doc = word.Documents.Open(input_file)
doc.SaveAs(output_file, FileFormat=wdFormatPDF)
doc.Close()
word.Quit()

if __name__ == "__main__":

dir_word = "D:\\工作-----------------------------------------\\知识库\\visual保持路径\\py测试目录\\DOCX" # word目录
dir_pdf = "D:\\工作-----------------------------------------\\知识库\\visual保持路径\\py测试目录\\PDF" # pdf存放目录

for root, dirs, filenames in os.walk(dir_word):
for file in filenames:

if file.endswith(".doc") :
doc2pdf( str(dir_word + "\\" + file), str(dir_pdf + "\\" + file.replace(".doc",".pdf")) )
elif file.endswith(".docx"):
doc2pdf( str(dir_word + "\\" + file), str(dir_pdf + "\\" + file.replace(".docx",".pdf")) )

sleep(1) # 每次间隔1s