docx转pdf

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}'.")
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容