Python调用Win32com实现Office批量转PDF

Python调用Win32com实现Office批量转PDF

需求

  • 一直以来有将诸如Word之类的Office文档转为PDF的需求,此前的方法是挨个打开文档,手动另存为PDF,此方法费时费力,尤其在电脑配置不高、同时运行很多程序的情况下会造成卡顿,因此需要通过Python将此类文档自动转为PDF,如果能够批量转换最好。

调研

  • 在网上搜索调研后,决定选择Win32com组件,因为它是微软的原生API,功能强大,手动操作Office能做到的通过调用Win32com一样能实现。缺陷是仅限于Win+Office环境。

  • 在原先需求的基础上增加PDF转PNG图片,经调研后选择使用PyMuPDF。

代码实现

  • Office转PDF
from win32com.client import Dispatch
from os import walk
import sys

def doc2pdf(input_file):
    word = Dispatch('Word.Application') # WPS改为Kwps.Application
    # word = DispatchEx('Word.Application')  # 启动独立进程
    output_file = input_file.split(".")
    try:
        doc = word.Documents.Open(input_file)
        doc.SaveAs(output_file[0] + ".pdf", FileFormat=17) # Word另存为PDF为17
        doc.Close()
    except:
        print("Unexpected error:", sys.exc_info())
    word.Quit()

def ppt2pdf(input_file):
    powerpoint = Dispatch('Powerpoint.Application') # WPS改为Kwpp.Application
    output_file = input_file.split(".")
    try:
        ppt = powerpoint.Presentations.Open(input_file)
        ppt.SaveAs(output_file[0] + ".pdf", FileFormat=32) # PPT另存为PDF为32
        ppt.Close()
    except:
        print("Unexpected error:", sys.exc_info())
    powerpoint.Quit()

def xls2pdf(input_file):
    excel = Dispatch('Excel.Application') # WPS改为Ket.Application
    output_file = input_file.split(".")
    try:
        xls = excel.Workbooks.Open(input_file)
        xls.SaveAs(output_file[0] + ".pdf", FileFormat=57) # Excel另存为PDF为57
        xls.Close()
    except:
        print("Unexpected error:", sys.exc_info())
    excel.Quit()

if __name__ == "__main__":
    doc_files = []
    directory = "C:\\Users\\Administrator\\Desktop"
    # 对directory目录里的所有文件进行遍历
    for root, dirs, filenames in walk(directory):
        for file in filenames:
            # 忽略~$开头的临时文件,并根据后缀名判断文件类型
            if file.find("~$") == -1:
                if file.endswith(".doc") or file.endswith(".docx") or file.endswith(".DOC"):
                    doc2pdf(str(root + "\\" + file))
                elif file.endswith(".ppt") or file.endswith(".pptx") or file.endswith(".PPT"):
                    ppt2pdf(str(root + "\\" + file))
                elif file.endswith(".xls") or file.endswith(".xlsx") or file.endswith(".XLS"):
                    xls2pdf(str(root + "\\" + file))
  • PDF转png
from os import walk
import sys
import fitz

'''
# 将PDF转化为图片
input_file pdf文件的路径
zoom_x x方向的缩放系数
zoom_y y方向的缩放系数
rotation_angle 旋转角度
'''

def pdftoimage(input_file,zoom_x,zoom_y,rotation_angle):
    try:
        # 打开PDF文件
        pdf = fitz.open(input_file)
        # 逐页读取PDF
        for pg in range(0, pdf.pageCount):
            page = pdf[pg]
            # 设置缩放和旋转系数
            trans = fitz.Matrix(zoom_x, zoom_y).preRotate(rotation_angle)
            pm = page.getPixmap(matrix=trans, alpha=False)
            # 开始写图像
            pm.writePNG(input_file.replace(".pdf","")+str(pg+1)+".png")
        pdf.close()
    except:
        print("Unexpected error:", sys.exc_info())

if __name__ == "__main__":
    doc_files = []
    directory = "C:\\Users\\Administrator\\Desktop"
    for root, dirs, filenames in walk(directory):
        for file in filenames:
            if file.endswith(".pdf"):
                pdftoimage(str(root + "\\" + file),2,2,0)

注意事项

  • 调用Win32com对Office文档进行操作与人工手动打开Office文档无异,优点在不用手动点击鼠标

  • 此代码根据对应目录下的文件挨个转换,未支持异步多线程转换

  • 使用DispatchEx可启动独立进程进行转换,优点在于不影响正在编辑的文档,缺点在于如果转换出错会造成进程残留

  • 代码运行中可能看到Word/Excel/PowerPoint窗口显示或错误警告,可添加word.Visible = 0隐藏窗口,添加word.DisplayAlerts = 0忽略错误警告

  • 加入try/except是为了防止转换异常导致进程未关闭,从而造成Office无法打开的情况发生

  • Word、Excel、PowerPoint另存为PDF的FileFormat不同,Word为17、Excel为57、PowerPoint为32

  • 如果转换的同时有对应的Word/Excel/PowerPoint实例在运行,转换完毕后的.Quit()方法会将这些实例一并关闭,请提前做好保存,或使用DispatchEx启动独立进程,建议不要在运行代码的同时打开实例

  • PDF转PNG时一定要先安装fitz,再安装PyMuPDF,否则会报错

参考资料

posted @ 2021-01-03 22:47  Lolipop  阅读(1980)  评论(0编辑  收藏  举报