def docx_to_pdf(input_file_path, custom_name=None):
output_file_path = "D://监管规则项目/2023311pdf/"
if not os.path.exists(input_file_path):
raise FileNotFoundError(f"输入的文件 '{input_file_path}' 不存在。")
if not input_file_path.endswith('.docx'):
raise ValueError("输入的文件必须是一个 '.docx' 格式的文件。")
if custom_name is not None:
output_dir = os.path.dirname(output_file_path)
pdf_file = f"{custom_name}.pdf"
output_file_path = os.path.join(output_dir, pdf_file)
# Ensure that the output directory exists
os.makedirs(os.path.dirname(output_file_path), exist_ok=True)
word_app = comtypes.client.CreateObject('Word.Application')
word_app.Visible = False
doc = word_app.Documents.Open(input_file_path)
doc.SaveAs(output_file_path, FileFormat=17) # 17 corresponds to the PDF format in Word
doc.Close()
word_app.Quit()
print(f"文件 '{input_file_path}' 已成功转换为 '{output_file_path}'.")
docx转pdf
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- Windows 下安装 Pandoc pandoc -v 查看版本 pdf 转换为 docx pdf2doc 在线...
- python Word文件转换为pdf文件(doc/docx文件转化为pdf文件) python doc转docx...
- 前言 对于PDF转换成word文档,我想很多人都了解过,那就是需要付费,而且很贵,但是如果你会Python,只要你...