如何使用Python批量docx转pdf?

如何使用Python批量docx转pdf?

1. 安装相关Python包

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple python-docx pywin32

2. 测试单个文件转换

import os
import win32com.client
from docx import Document

def convert_to_pdf(docx_file, pdf_file):
    # 打开DOCX文件
    doc = Document(docx_file)

    # 将DOCX文件保存为临时的HTML文件
    temp_html = "temp.html"
    doc.save(temp_html)

    # 创建PDF转换器对象
    word_app = win32com.client.Dispatch("Word.Application")
    word_app.Visible = False

    # 打开临时HTML文件
    doc_pdf = word_app.Documents.Open(os.path.abspath(temp_html))

    # 将临时HTML文件保存为PDF
    doc_pdf.SaveAs(os.path.abspath(pdf_file), FileFormat=17)

    # 关闭文档和应用程序
    doc_pdf.Close()
    word_app.Quit()

    # 删除临时HTML文件
    os.remove(temp_html)

# 调用函数进行转换
convert_to_pdf(r"C:\Users\lishi\Desktop\测试一.docx", r"C:\Users\lishi\Desktop\测试一.pdf")

3.批量文件转换

import os
import win32com.client
from docx import Document

def convert_to_pdf(docx_file, pdf_file):
    # 打开DOCX文件
    doc = Document(docx_file)

    # 将DOCX文件保存为临时的HTML文件
    temp_html = "temp.html"
    doc.save(temp_html)

    # 创建PDF转换器对象
    word_app = win32com.client.Dispatch("Word.Application")
    word_app.Visible = False

    # 打开临时HTML文件
    doc_pdf = word_app.Documents.Open(os.path.abspath(temp_html))

    # 将临时HTML文件保存为PDF
    output_pdf_file = os.path.splitext(docx_file)[0] + ".pdf"
    doc_pdf.SaveAs(os.path.abspath(output_pdf_file), FileFormat=17)

    # 关闭文档和应用程序
    doc_pdf.Close()
    word_app.Quit()

    # 删除临时HTML文件
    os.remove(temp_html)

# 指定待转换的文件夹路径
folder_path = r"C:\Users\lishi\Desktop\test"

# 获取文件夹下所有的文件
file_list = os.listdir(folder_path)

# 遍历文件列表
for file_name in file_list:
    # 检查文件扩展名是否为.docx
    if file_name.endswith(".docx"):
        # 构建文件的完整路径
        file_path = os.path.join(folder_path, file_name)

        # 调用函数进行转换
        convert_to_pdf(file_path, file_path.replace(".docx", ".pdf"))

在这里插入图片描述

转换后的效果图:
在这里插入图片描述

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用以下代码将docx文件批量换为pdf文件: ```python import os from docx2pdf import convert def docx2pdf(input_path): docx_files = \[file for file in os.listdir(input_path) if file.endswith('.docx')\] for docx_file in docx_files: print("开始换 -> " + docx_file) convert(input_path + '\\' + docx_file) if __name__ == '__main__': docx2pdf('C:/Users/1223/Desktop/OK') ``` 这段代码使用了`docx2pdf`库来实现批量换。首先,通过`os.listdir`函数获取指定目录下的所有文件,然后筛选出以`.docx`结尾的文件。接下来,使用`convert`函数将每个docx文件换为pdf文件。最后,调用`docx2pdf`函数并传入目标文件夹的路径即可实现批量换。 #### 引用[.reference_title] - *1* *3* [如何用Python批量Pdf文档换成Word文档?](https://blog.csdn.net/weixin_55609727/article/details/127606700)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Python 批量将 Word 换为 PDF](https://blog.csdn.net/weixin_67276852/article/details/130413085)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值