Python docx、Excel、PPT转pdf

1、docx转pdf

下载

python -m pip install docx2pdf

使用

import os

from docx2pdf import convert


def __create_folder(folder):
    folder = os.path.abspath(folder)
    if not os.path.exists(folder):
        try:
            os.makedirs(folder)
            msg = 'Success create folder({})'.format(folder)
            print(msg)
        except Exception as e:
            msg = 'Failed create folder({}), exception({})'.format(folder, e)
            print(msg)


def docx_to_pdf(docx_name, pdf_name):
    docx_name = os.path.abspath(docx_name)
    pdf_name = os.path.abspath(pdf_name)
    __create_folder(os.path.dirname(pdf_name))
    if os.path.isfile(docx_name):
        try:
            convert(docx_name, pdf_name)
            msg = 'Success convert docx to pdf, docx path "{}", ' \
                  'pdf path "{}"'.format(docx_name, pdf_name)
            print(msg)
        except Exception as e:
            msg = 'Fail convert docx to pdf, docx path "{}", ' \
                  'pdf path "{}" exception "{}"'.format(docx_name, pdf_name, e)
            print(msg)


def debug():
    docx_to_pdf("Docx_Example.docx", "a.pdf")


if __name__ == '__main__':
    debug()

2、Excel转pdf

import os

from win32com import client


def __create_folder(folder):
    folder = os.path.abspath(folder)
    if not os.path.exists(folder):
        try:
            os.makedirs(folder)
            msg = 'Success create folder({})'.format(folder)
            print(msg)
        except Exception as e:
            msg = 'Failed create folder({}), exception({})'.format(folder, e)
            print(msg)


def excel_to_pdf(excel_name, pdf_name):
    excel_name = os.path.abspath(excel_name)
    pdf_name = os.path.abspath(pdf_name)
    __create_folder(os.path.dirname(pdf_name))
    try:
        xlApp = client.Dispatch("Excel.Application")
        books = xlApp.Workbooks.Open(excel_name)
        books.ExportAsFixedFormat(0, pdf_name)
        xlApp.Quit()
        msg = 'Success convert excel to pdf, ' \
              'excel path "{}", pdf path "{}"'.format(excel_name, pdf_name)
        print(msg)
    except Exception as e:
        msg = 'Fail convert excel to pdf, excel path "{}", pdf path "{}" ' \
              'exception "{}"'.format(excel_name, pdf_name, e)
        print(msg)


def debug():
    excel_to_pdf(os.path.abspath("Excel_Example.xlsx"), os.path.abspath("a/a1.pdf"))


if __name__ == '__main__':
    debug()

3、ppt转pdf

import os

from win32com.client import gencache


def __create_folder(folder):
    folder = os.path.abspath(folder)
    if not os.path.exists(folder):
        try:
            os.makedirs(folder)
            msg = 'Success create folder({})'.format(folder)
            print(msg)
        except Exception as e:
            msg = 'Failed create folder({}), exception({})'.format(folder, e)
            print(msg)


def ppt_to_pdf(ppt_name, pdf_name):
    ppt_name = os.path.abspath(ppt_name)
    pdf_name = os.path.abspath(pdf_name)
    __create_folder(os.path.dirname(pdf_name))
    try:
        p = gencache.EnsureDispatch("PowerPoint.Application")
        ppt = p.Presentations.Open(ppt_name, False, False, False)
        ppt.ExportAsFixedFormat(pdf_name, 2, PrintRange=None)
        p.Quit()
        msg = 'Success convert ppt to pdf, ' \
              'ppt path "{}", pdf path "{}"'.format(ppt_name, pdf_name)
        print(msg)
    except Exception as e:
        msg = 'Fail convert ppt to pdf, ppt path "{}", pdf path "{}" ' \
              'exception "{}"'.format(ppt_name, pdf_name, e)
        print(msg)


def debug():
    ppt_to_pdf(os.path.abspath("PPTX_Example.pptx"), os.path.abspath("a/a1.pdf"))


if __name__ == '__main__':
    debug()

学习链接:

https://www.jianshu.com/p/020f4f62beb9

https://www.cnblogs.com/Iloveyy/p/13304073.html

https://www.codeleading.com/article/9032753153/

posted @ 2021-07-08 10:11  南风丶轻语  阅读(581)  评论(0编辑  收藏  举报