将.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(' ')
相关文章
相关标签/搜索