python中word转pdf

目标

将doc和docx文件转换成pdf格式

代码

word = Dispatch('Word.Application')
for dirpath, dirnames, filenames in walk(Address): # address是文件夹地址
    # 判断有没有文件
    if filenames==[]:
        print("文件夹为空,请检查!")
    # 判断是不是含有.doc或者.docx文件
    elif ".doc" or ".docx" in filenames:
        for f in filenames:
            if f.lower().endswith(".docx"):
                new_name = f.replace(".docx", ".pdf")
                in_file =(dirpath + '/'+ f)
                new_file =(dirpath + '/' + new_name)
                doc = word.Documents.Open(in_file)
                doc.SaveAs(new_file, FileFormat = 17)
                print("完成.docx到.pdf的转换!")
                doc.Close()
            elif f.lower().endswith(".doc"):
                new_name = f.replace(".doc", ".pdf")
                in_file =(dirpath +'/' + f)
                new_file =(dirpath +'/' + new_name)
                doc = word.Documents.Open(in_file)
                doc.SaveAs(new_file, FileFormat = 17)
                print("完成.doc到.pdf的转换!")
                doc.Close()
word.Quit()

总结

代码说的很清楚了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值