将.docx文件转化为.pdf文件
在需要转化.docx为.pdf的文件夹中打开powershell然后运行该程序,可以将文件夹下所有.docx文件转化为.pdf文件。
from win32com.client import Dispatch, constants, gencache
import os
def doc2pdf(docPath, pdfPath):
docPathTrue = os.path.abspath(docPath)
pdfPathTrue = os.path.abspath(pdfPath)
#word = gencache.EnsureDispatch('Word.Application')
word = Dispatch('Word.Application')
doc = word.Documents.Open(docPathTrue, ReadOnly=1)
doc.ExportAsFixedFormat(pdfPathTrue,
constants.wdExportFormatPDF,
Item=constants.wdExportDocumentWithMarkup,
CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
word.Quit(constants.wdDoNotSaveChanges)
for foldername, subfolders, filenames in os.walk('.\\'):
for i in filenames:
if '.docx' in i:
doc2pdf(foldername + '\\'+ i ,foldername + '\\'+ os.path.splitext(i)[0]+'.pdf')
print(foldername + '\\'+ i )
print(' ')