python实现WPS文件转PDF

实现WPS文件转PDF,需要安装金山WPS

import os

from win32com.client import Dispatch


def wps_et_to_pdf(file, pdf_file=None):
    """
    wps/et 转 PDF
    :param file: wps/et文件路径
    :param pdf_file: pdf文件路径
    :return: pdf_file,PDF路径
    """
    # 生成的PDF路径
    if not pdf_file:
        pdf_file = f'{os.path.splitext(file)[0]}.pdf'
    if file.endswith('.wps'):
        app = Dispatch("Kwps.Application")  # Kwps报错,版本问题导致,可以尝试用wps
        app.Visible = False
        wps = app.Documents.Open(file)
        wps.ExportAsFixedFormat(pdf_file, 17)
    else:
        app = Dispatch("Ket.Application")  # 同上,可以尝试用et
        wps = app.Workbooks.Open(file)
        wps.ExportAsFixedFormat(0, pdf_file)
    wps.Close()
    app.Quit()
    return pdf_file
posted @ 2021-07-01 17:06  cnblogs用户  阅读(864)  评论(0编辑  收藏  举报