python doc、ppt、excel转pdf

简短的Python代码,可实现批量地将微软ppt、doc、excel文件转换为pdf格式,具有一定的参考意义。

import comtypes.client
import os 

def ppt_pdf(path):
	# PPT 转 PDF
    pdf_path = path.replace('ppt', 'pdf') # pdf保存路径 推荐使用绝对路径
    try:
        p = client.CreateObject("PowerPoint.Application")
        ppt = p.Presentations.Open(path)
        ppt.ExportAsFixedFormat(pdf_path, 2, PrintRange=None)
        ppt.Close()
        p.Quit()
    except Exception as e:
        pass
 

def word_pdf(path):
	# Word转pdf
    pdf_path = path.replace('doc', 'pdf')
    w = client.CreateObject("Word.Application")
    doc = w.Documents.Open(path)
    doc.ExportAsFixedFormat(pdf_path, 17)
    doc.Close()
    w.Quit()


def excel_pdf(self, path):
	# Excel转pdf
    pdf_path = path.replace('xls', 'pdf')
    xlApp = client.CreateObject("Excel.Application")
    books = xlApp.Workbooks.Open(path)
    books.ExportAsFixedFormat(0, pdf_path)
    xlApp.Quit()
 

推荐使用绝对路径

你可能感兴趣的:(python doc、ppt、excel转pdf)