欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > 用python把docx批量转为pdf

用python把docx批量转为pdf

2024/10/25 6:21:50 来源:https://blog.csdn.net/oHanTanYanYing/article/details/139623591  浏览:    关键词:用python把docx批量转为pdf

为保证转换质量,本文的方法是通过脚本和com技术调用office自带的程序进行转换的,因此需要电脑已经装有office。如果希望不装office也能用,则需要研究OpenXML技术,后面实在闲的慌(退休)再搞。

安装所需库

pip install comtypes

示例代码

# Copy from Yue Zhangimport os
import comtypes.clientdef convert_word_to_pdf(folder_path):word = comtypes.client.CreateObject("Word.Application")word.Visible = Falsefor file in os.listdir(folder_path):if file.endswith(".docx") or file.endswith(".doc"):in_file = os.path.abspath(os.path.join(folder_path, file))out_file = os.path.abspath(os.path.join(folder_path, file.replace(".docx", ".pdf").replace(".doc", ".pdf")))doc = word.Documents.Open(in_file)doc.SaveAs(out_file, FileFormat=17) # 17 代表 PDF 格式doc.Close()word.Quit()# 使用示例
folder_path = '存放docx或者doc文件的文件夹路径'
convert_word_to_pdf(folder_path)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com