Convert Word files to PDF files in batches using Python

Note: When using Minio server, you cannot preview word files. If necessary, you can convert word files into pdf files and then store them in Minio. This article introduces how to batch convert word files into pdf format files;

Insert image description here

Installation library

First, you need to install a library, pywin32;

Insert image description here

You can type the following command in the cmd window to install and use the Alibaba Cloud image:

pip install pywin32 -i https://mirrors.aliyun.com/pypi/simple/

If you are using pycharm, I recommend installing it directly in the software, as follows:

Insert image description here

coding

code show as below:

import os
import time

import win32com.client


def convert_to_pdf(input_path, output_path):
    # 使用win32com对象打开Word应用程序
    word = win32com.client.Dispatch("Word.Application")

    # 去除程序界面显示
    word.Visible = 0

    # 打开Word文档
    doc = word.Documents.Open(input_path)

    # 将Word文档保存为PDF文件
    doc.SaveAs(output_path, FileFormat=17)

    # 关闭Word文档
    doc.Close()

    # 关闭Word应用程序
    word.Quit()


def main(input_path, output_path, file):
    try:
        # 转换为绝对路径
        input_path = os.path.abspath(input_path + "\\" + file)

        if file[-4:] == "docx":
            output_path = os.path.abspath(output_path + "\\" + file[:-5] + ".pdf")
        else:
            output_path = os.path.abspath(output_path + "\\" + file[:-4] + ".pdf")

        # 调用函数进行转换
        convert_to_pdf(input_path, output_path)
        print("转换成功!")
    except Exception as e:
        print(f"转换失败: {
      
      str(e)}")


if __name__ == "__main__":
    # 输入路径
    input_path = r""

    # 输出路径
    output_path = r""

    # 获取输入路径下的所有文件
    listdir = os.listdir(input_path)

    # 遍历所有文件
    for file in listdir:

        # 判断是否为Word文档
        if file[-4:] == "docx" or file[-3:] == "doc":
            main(input_path, output_path, file)

            # 休眠2秒,防止Word应用程序未关闭就进行下一次转换
            time.sleep(2)

test

For example, there is a word file in the test folder on the desktop;

Insert image description here

Start the program and perform conversion;

Insert image description here

Conversion completed;

Insert image description here

Guess you like

Origin blog.csdn.net/qq_42108331/article/details/132347351