网站建设广告投放是什么,丽水网站建设费用,wordpress调用二级分类,网站建设市场调查报告说明#xff1a;在使用Minio服务器时#xff0c;无法对word文件预览#xff0c;如果有需要的话#xff0c;可以将word文件转为pdf文件#xff0c;再存储到Minio中#xff0c;本文介绍如何批量将word文件#xff0c;转为pdf格式的文件#xff1b; 安装库
首先#xff…说明在使用Minio服务器时无法对word文件预览如果有需要的话可以将word文件转为pdf文件再存储到Minio中本文介绍如何批量将word文件转为pdf格式的文件 安装库
首先需要安装一个库pywin32 可以在cmd窗口敲下面的命令安装使用阿里云镜像
pip install pywin32 -i https://mirrors.aliyun.com/pypi/simple/如果你使用的是pycharm我建议直接在软件里安装如下 编码
代码如下
import os
import timeimport win32com.clientdef 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, FileFormat17)# 关闭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文件夹里有一个word文件 启动程序进行转换 转换完成