python 实现 批量转换word 为pdf 文档

首先安装 win32com模块

但是在cmd中输入pip install win32com安装不成功。
解决办法:输入python -m pip install pypiwin32进行安装,成功解决。

 

from win32com.client import gencache
from win32com.client import constants, gencache
import os


def createPdf(wordPath, pdfPath):
    """
    word转pdf
    :param wordPath: word文件路径
    :param pdfPath:  生成pdf文件路径
    """
    word = gencache.EnsureDispatch('Word.Application')
    doc = word.Documents.Open(wordPath, ReadOnly=1)
    doc.ExportAsFixedFormat(pdfPath,
                            constants.wdExportFormatPDF,
                            Item=constants.wdExportDocumentWithMarkup,
                            CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
    word.Quit(constants.wdDoNotSaveChanges)

sourcePath="C:/Users/Administrator/Desktop/word"
files=os.listdir(sourcePath)

for file in files:
    file = sourcePath+'/'+file

    pdfpath=file+'.pdf'
    print(pdfpath)
    createPdf(file,pdfpath)

哪位大佬打包好了这个程序的话可以发我哈哈,不客气

你可能感兴趣的:(python)