说明:
1、因为项目需要,需要手动将十几个word文档转换成pdf文档
2、python请安装3.9.0以上,否则一些依赖库无法正常用
#! /usr/bin/python3
# -*- coding: utf-8 -*-import os
import comtypes.client# 批量将docx文件转换pdf文件
def docx_to_pdf(docx_path, pdf_path):word = comtypes.client.CreateObject('Word.Application')doc = word.Documents.Open(docx_path)doc.SaveAs(pdf_path, FileFormat=17)doc.Close()word.Quit()# 批量获取docx文件
def get_folder_name(folder_path):for filename in os.listdir(folder_path):if filename.endswith('.docx'):docx_path = os.path.join(folder_path, filename)pdf_path = os.path.join(folder_path, os.path.splitext(filename)[0] + '.pdf')docx_to_pdf(docx_path, pdf_path)elif filename.endswith('.doc'):docx_path = os.path.join(folder_path, filename)pdf_path = os.path.join(folder_path, os.path.splitext(filename)[0] + '.pdf')docx_to_pdf(docx_path, pdf_path)if __name__ == '__main__':# docx文档路径folder_path = r"F:/移动公司/项目验收/智慧停车项目/项目验收资料/"get_folder_name(folder_path)