將.docx文件轉化爲.pdf文件

將.docx文件轉化爲.pdf文件

在須要轉化.docx爲.pdf的文件夾中打開powershell而後運行該程序,能夠將文件夾下全部.docx文件轉化爲.pdf文件。shell

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(' ')
相關文章
相關標籤/搜索