Bootstrap

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()

总结

代码说的很清楚了

;