美文网首页
docx转pdf

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