将.docx文件转化为.pdf文件

六眼飞鱼酱① 提交于 2019-12-05 03:56:10

将.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(' ')

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!