docx转pdf
作者:
月夜星空下 | 来源:发表于
2023-03-30 14:27 被阅读0次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
本文链接:https://www.haomeiwen.com/subject/oafcddtx.html
网友评论