docx批量转pdf123456789101112131415161718192021222324252627282930 from win32com.client import Dispatchimport osfrom time import sleepwdFormatPDF = 17def doc2pdf(input_file,output_file): print(input_file) print(output_file) word = Dispatch('Word.Application') doc = word.Documents.Open(input_file) doc.SaveAs(output_file, FileFormat=wdFormatPDF) doc.Close() word.Quit()if __name__ == "__main__": dir_word = "D:\\工作-----------------------------------------\\知识库\\visual保持路径\\py测试目录\\DOCX" # word目录 dir_pdf = "D:\\工作-----------------------------------------\\知识库\\visual保持路径\\py测试目录\\PDF" # pdf存放目录 for root, dirs, filenames in os.walk(dir_word): for file in filenames: if file.endswith(".doc") : doc2pdf( str(dir_word + "\\" + file), str(dir_pdf + "\\" + file.replace(".doc",".pdf")) ) elif file.endswith(".docx"): doc2pdf( str(dir_word + "\\" + file), str(dir_pdf + "\\" + file.replace(".docx",".pdf")) ) sleep(1) # 每次间隔1s