python实现word/excel/ppt批量转pdf的示例代码

 更新时间:2023年09月05日 08:39:49   作者:夏天是冰红茶  
这篇文章主要为大家详细介绍了如何利用python实现word、excel、ppt批量转pdf文件,文中的示例代码讲解详细,有需要的小伙伴可以参考下

今天看见了一个有意思的脚本Python批量实现Word、EXCLE、PPT转PDF文件

因为我平时word用的比较的多,所以深有体会,具体怎么实现的我们就不讨论了,因为这个去学了也没什么提升,不然也不会当作脚本了。这里我将其放入了pyzjr库中,也方便大家进行调用。

你可以去下载pyzjr:

pip install pyzjr -i https://pypi.tuna.tsinghua.edu.cn/simple

调用方法:

import pyzjr as pz
# 实例化对象
Mpdf = pz.Microsoft2PDF()
# 调用类的方法
Mpdf.Word2Pdf()  # word -> pdf
Mpdf.Excel2Pdf()  # excel -> pdf
Mpdf.PPt2Pdf()  # ppt -> pdf
Mpdf.WEP2Pdf()  # word,excel,ppt -> pdf

上面就是api的调用了,统一会将文件存放在目标文件夹下新建的名为pdf文件夹中。

pyzjr中的源码:

import win32com.client, gc, os
class Microsoft2PDF():
    """Convert Microsoft Office documents (Word, Excel, PowerPoint) to PDF format"""
    def __init__(self,filePath = ""):
        """
        :param filePath: 如果默认是空字符,就默认当前路径
        """
        self.flagW = self.flagE = self.flagP = 1
        self.words = []
        self.ppts = []
        self.excels = []
        if filePath == "":
            filePath = os.getcwd()
        folder = filePath + '\\pdf\\'
        self.folder = CreateFolder(folder,debug=False)
        self.filePath = filePath
        for i in os.listdir(self.filePath):
            if i.endswith(('.doc', 'docx')):
                self.words.append(i)
            if i.endswith(('.ppt', 'pptx')):
                self.ppts.append(i)
            if i.endswith(('.xls', 'xlsx')):
                self.excels.append(i)
        if len(self.words) < 1:
            print("\n[pyzjr]:No Word files\n")
            self.flagW = 0
        if len(self.ppts) < 1:
            print("\n[pyzjr]:No PPT file\n")
            self.flagE = 0
        if len(self.excels) < 1:
            print("\n[pyzjr]:No Excel file\n")
            self.flagP = 0
    def Word2Pdf(self):
        if self.flagW == 0:
            return 0
        else:
            print("\n[Start Word ->PDF conversion]")
            try:
                print("Open Word Process...")
                word = win32com.client.Dispatch("Word.Application")
                word.Visible = 0
                word.DisplayAlerts = False
                doc = None
                for i in range(len(self.words)):
                    print(i)
                    fileName = self.words[i]  # file name
                    fromFile = os.path.join(self.filePath, fileName)  # file address
                    toFileName = self.changeSufix2Pdf(fileName)  # Generated file name
                    toFile = self.toFileJoin(toFileName)  # Generated file address
                    print("Conversion:" + fileName + "in files...")
                    try:
                        doc = word.Documents.Open(fromFile)
                        doc.SaveAs(toFile, 17)
                        print("Convert to:" + toFileName + "file completion")
                    except Exception as e:
                        print(e)
                print("All Word files have been printed")
                print("End Word Process...\n")
                doc.Close()
                doc = None
                word.Quit()
                word = None
            except Exception as e:
                print(e)
            finally:
                gc.collect()
    def Excel2Pdf(self):
        if self.flagE == 0:
            return 0
        else:
            print("\n[Start Excel -> PDF conversion]")
            try:
                print("open Excel Process...")
                excel = win32com.client.Dispatch("Excel.Application")
                excel.Visible = 0
                excel.DisplayAlerts = False
                wb = None
                ws = None
                for i in range(len(self.excels)):
                    print(i)
                    fileName = self.excels[i]
                    fromFile = os.path.join(self.filePath, fileName)
                    print("Conversion:" + fileName + "in files...")
                    try:
                        wb = excel.Workbooks.Open(fromFile)
                        for j in range(wb.Worksheets.Count):  # Number of worksheets, one workbook may have multiple worksheets
                            toFileName = self.addWorksheetsOrder(fileName, j + 1)
                            toFile = self.toFileJoin(toFileName)
                            ws = wb.Worksheets(j + 1)
                            ws.ExportAsFixedFormat(0, toFile)
                            print("Convert to:" + toFileName + "file completion")
                    except Exception as e:
                        print(e)
                # 关闭 Excel 进程
                print("All Excel files have been printed")
                print("Ending Excel process...\n")
                ws = None
                wb.Close()
                wb = None
                excel.Quit()
                excel = None
            except Exception as e:
                print(e)
            finally:
                gc.collect()
    def PPt2Pdf(self):
        if self.flagP == 0:
            return 0
        else:
            print("\n[Start PPT ->PDF conversion]")
            try:
                print("Opening PowerPoint process...")
                powerpoint = win32com.client.Dispatch("PowerPoint.Application")
                ppt = None
                for i in range(len(self.ppts)):
                    print(i)
                    fileName = self.ppts[i]
                    fromFile = os.path.join(self.filePath, fileName)
                    toFileName = self.changeSufix2Pdf(fileName)
                    toFile = self.toFileJoin(toFileName)
                    print("Conversion:" + fileName + "in files...")
                    try:
                        ppt = powerpoint.Presentations.Open(fromFile, WithWindow=False)
                        if ppt.Slides.Count > 0:
                            ppt.SaveAs(toFile, 32)
                            print("Convert to:" + toFileName + "file completion")
                        else:
                            print("Error, unexpected: This file is empty, skipping this file")
                    except Exception as e:
                        print(e)
                print("All PPT files have been printed")
                print("Ending PowerPoint process...\n")
                ppt.Close()
                ppt = None
                powerpoint.Quit()
                powerpoint = None
            except Exception as e:
                print(e)
            finally:
                gc.collect()
    def WEP2Pdf(self):
        """
        Word, Excel and PPt are all converted to PDF.
        If there are many files, it may take some time
        """
        print("Convert Microsoft Three Musketeers to PDF")
        self.Word2Pdf()
        self.Excel2Pdf()
        self.PPt2Pdf()
        print(f"All files have been converted, you can find them in the {self.folder}")
    def changeSufix2Pdf(self,file):
        """将文件后缀更改为.pdf"""
        return file[:file.rfind('.')] + ".pdf"
    def addWorksheetsOrder(self,file, i):
        """在文件名中添加工作表顺序"""
        return file[:file.rfind('.')] + "_worksheet" + str(i) + ".pdf"
    def toFileJoin(self, file):
        """将文件路径和文件名连接为完整的文件路径"""
        return os.path.join(self.filePath, 'pdf', file[:file.rfind('.')] + ".pdf")

这里我对原先博主的代码进行了一定的优化,使其可供我们调用。

这是控制台打印出来的信息,我们可以发现在调用WEP2Pdf时,如果当前文件夹中没有word的文件也能继续去转换。 

到此这篇关于python实现word/excel/ppt批量转pdf的示例代码的文章就介绍到这了,更多相关python pdf内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • ubuntu 18.04 安装opencv3.4.5的教程(图解)

    ubuntu 18.04 安装opencv3.4.5的教程(图解)

    这篇文章主要介绍了ubuntu 18.04 安装opencv3.4.5的教程,本文图文并茂给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-11-11
  • Python Tkinter Menu控件使用详解

    Python Tkinter Menu控件使用详解

    Menu控件(菜单控件)可以说是 GUI 中“精髓所在”,它以可视化的方式将一系列的命令进行分组,在每一个分组下又可以“隐藏”许多的程序执行命令(即功能)。本文将详细介绍它的使用,需要的可以参考一下
    2022-01-01
  • pandas每次多Sheet写入文件的方法

    pandas每次多Sheet写入文件的方法

    今天小编就为大家分享一篇pandas每次多Sheet写入文件的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12
  • Python获取SQLite查询结果表列名的方法

    Python获取SQLite查询结果表列名的方法

    这篇文章主要介绍了Python获取SQLite查询结果表列名的方法,涉及Python连接及查询SQLite数据库的相关操作技巧,需要的朋友可以参考下
    2017-06-06
  • python字符串分割及字符串的一些常规方法

    python字符串分割及字符串的一些常规方法

    这篇文章主要介绍了python字符串分割以及字符串的一些常规方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-07-07
  • 教你在Excel中调用Python脚本实现数据自动化处理的方法

    教你在Excel中调用Python脚本实现数据自动化处理的方法

    Excel是全世界最流行的编程语言,Excel已经可以实现编程语言的算法,因此它是具备图灵完备性的,和JavaScript、Java、Python一样,今天通过本文给大家介绍下Python数据自动化处理的相关知识,感兴趣的朋友一起看看吧
    2022-02-02
  • Python根据字符串调用函数过程解析

    Python根据字符串调用函数过程解析

    这篇文章主要介绍了Python根据字符串调用函数过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • python 动态加载的实现方法

    python 动态加载的实现方法

    脚本语言都有一个优点,就是动态加载,python也有这个特性。这篇文章主要介绍了python 动态加载的实现方法,需要的朋友可以参考下
    2017-12-12
  • 基于python+selenium自动健康打卡的实现代码

    基于python+selenium自动健康打卡的实现代码

    这篇文章主要介绍了基于python+selenium自动健康打卡,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • 一文讲清Python中文件路径的基本概念与操作方法

    一文讲清Python中文件路径的基本概念与操作方法

    这篇文章主要为大家详细介绍了Python中文件路径的基本概念与操作方法,其中文件路径主要分为绝对路径(从根目录开始)和相对路径(相对于当前工作目录),感兴趣的小伙伴可以了解下
    2026-04-04

最新评论